In C#, data types specify the kind of data a variable can hold. They help ensure that the values stored in variables are compatible with operations that can be performed on them. C# has two main categories of data types: value types and reference types.
1. Value Types
Value types hold the actual data directly, and when they are assigned to another variable, a copy of the data is made.
- Primitive Value Types: These include numeric types, Boolean, and character types.
Common Value Types:
Data Type | Description | Default Value | Example |
---|---|---|---|
int | Represents 32-bit signed integers | 0 | int age = 30; |
long | Represents 64-bit signed integers | 0 | long distance = 1000000L; |
short | Represents 16-bit signed integers | 0 | short height = 160; |
byte | Represents 8-bit unsigned integers | 0 | byte level = 5; |
float | Represents single-precision floating-point numbers (32-bit) | 0.0f | float pi = 3.14f; |
double | Represents double-precision floating-point numbers (64-bit) | 0.0 | double salary = 5000.75; |
decimal | Represents high-precision decimal numbers for financial calculations (128-bit) | 0.0m | decimal price = 19.99m; |
char | Represents a single 16-bit Unicode character | '\0' | char grade = 'A'; |
bool | Represents a Boolean value (true or false) | false | bool isActive = true; |
Example Usage of Value Types:
using System;
class Program
{
static void Main()
{
// Integer example
int age = 30;
Console.WriteLine("Age: " + age);
// Float example
float pi = 3.14f;
Console.WriteLine("Pi: " + pi);
// Boolean example
bool isActive = true;
Console.WriteLine("Is Active: " + isActive);
}
}
Output:
Age: 30
Pi: 3.14
Is Active: True
2. Reference Types
Reference types store a reference (or address) to the location of the data in memory rather than the actual data. When you assign a reference type to another variable, you are copying the reference, not the actual data, which means changes to the second variable will affect the original data.
Common Reference Types:
Data Type | Description | Default Value | Example |
---|---|---|---|
string | Represents a sequence of characters | null | string name = "John"; |
object | The base type of all types in C# | null | object obj = 10; |
dynamic | Represents data whose type is determined at runtime | null | dynamic dynVar = 20; |
Example Usage of Reference Types:
using System;
class Program
{
static void Main()
{
// String example
string name = "John";
Console.WriteLine("Name: " + name);
// Object example (can hold any data type)
object obj = 123;
Console.WriteLine("Object: " + obj);
// Dynamic example (can change type at runtime)
dynamic value = 10;
Console.WriteLine("Dynamic Value: " + value);
value = "Hello, world!";
Console.WriteLine("Dynamic Value Changed: " + value);
}
}
Output:
Name: John
Object: 123
Dynamic Value: 10
Dynamic Value Changed: Hello, world!
3. Nullable Value Types
C# allows value types (such as int
, double
, etc.) to represent null using the ?
symbol. This is useful when you need to handle situations where a value might not be set (for example, in databases).
Example:
int? age = null; // Nullable int
Console.WriteLine(age.HasValue ? age.ToString() : "No value");
4. Type Inference with var
In C#, you can use the var
keyword to let the compiler infer the type of a variable based on its initial value. The type is determined at compile time, and you must assign a value at the time of declaration.
Example:
var number = 10; // Inferred as int
var name = "Alice"; // Inferred as string
var price = 19.99m; // Inferred as decimal
Note: The var
keyword does not mean the variable is dynamic—it is still statically typed.
5. Enumerations (Enums)
An enum is a special “class” that represents a group of constants (unchangeable variables).
Example:
enum Day { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday }
class Program
{
static void Main()
{
Day today = Day.Monday;
Console.WriteLine("Today is: " + today);
}
}
Output:
Today is: Monday
6. Arrays
An array is a collection of variables of the same type. Arrays in C# are reference types, even though they contain value types.
Example:
int[] numbers = { 1, 2, 3, 4, 5 };
Console.WriteLine("First number: " + numbers[0]);
Output:
First number: 1
Conclusion
Understanding C# data types is essential for writing robust, type-safe programs. By using appropriate data types for different operations, you ensure that your program behaves as expected and performs efficiently. Value types are faster and more efficient for small data, while reference types are useful for more complex data structures. You can also use nullable types, enums, and arrays for specialized scenarios.