Introduction
Data types in C# define the kind of data that a variable can hold. C# is a statically-typed language, which means every variable must have a defined data type at compile time. Understanding data types is essential for writing efficient and error-free code.
Categories of Data Types
C# data types can be broadly categorized into two types:
- Value Types
- Reference Types
Value Types
Value types hold data directly. They are stored in the stack and include simple types such as integers, floating-point numbers, and structures.
Common Value Types
- int: Represents a 32-bit signed integer.
- double: Represents a double-precision floating-point number.
- char: Represents a single 16-bit Unicode character.
- bool: Represents a Boolean value (
true
orfalse
).
Integer Types
byte
- Range: 0 to 255
- Size: 8 bits
byte age = 25;
Console.WriteLine("Age: " + age);
Output:
Age: 25
sbyte
- Range: -128 to 127
- Size: 8 bits
sbyte temperature = -10;
Console.WriteLine("Temperature: " + temperature);
Output:
Temperature: -10
short
- Range: -32,768 to 32,767
- Size: 16 bits
short population = 15000;
Console.WriteLine("Population: " + population);
Output:
Population: 15000
ushort
- Range: 0 to 65,535
- Size: 16 bits
ushort maxHeight = 60000;
Console.WriteLine("Max Height: " + maxHeight);
Output:
Max Height: 60000
int
- Range: -2,147,483,648 to 2,147,483,647
- Size: 32 bits
int salary = 50000;
Console.WriteLine("Salary: " + salary);
Output:
Salary: 50000
uint
- Range: 0 to 4,294,967,295
- Size: 32 bits
uint distance = 150000;
Console.WriteLine("Distance: " + distance);
Output:
Distance: 150000
long
- Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
- Size: 64 bits
long worldPopulation = 7800000000L;
Console.WriteLine("World Population: " + worldPopulation);
Output:
World Population: 7800000000
ulong
- Range: 0 to 18,446,744,073,709,551,615
- Size: 64 bits
ulong galaxyStars = 250000000000UL;
Console.WriteLine("Galaxy Stars: " + galaxyStars);
Output:
Galaxy Stars: 250000000000
Floating-Point Types
float
- Range: Approximately ±1.5e−45 to ±3.4e38
- Precision: 7 digits
- Size: 32 bits
float pi = 3.14F;
Console.WriteLine("PI: " + pi);
Output:
PI: 3.14
double
- Range: Approximately ±5.0e−324 to ±1.7e308
- Precision: 15-16 digits
- Size: 64 bits
double e = 2.718281828459045;
Console.WriteLine("E: " + e);
Output:
E: 2.718281828459045
decimal
- Range: Approximately ±1.0e−28 to ±7.9e28
- Precision: 28-29 digits
- Size: 128 bits
decimal price = 199.99M;
Console.WriteLine("Price: " + price);
Output:
Price: 199.99
Character Type
char
- Range: Any valid Unicode character
- Size: 16 bits
char grade = 'A';
Console.WriteLine("Grade: " + grade);
Output:
Grade: A
Boolean Type
bool
- Values:
true
orfalse
- Size: Depends on the target platform
bool isActive = true;
Console.WriteLine("Is Active: " + isActive);
Output:
Is Active: True
Reference Types
Reference types store references to their data (objects) in memory, rather than the actual data itself. They are stored in the heap and include types such as classes, interfaces, arrays, and delegates.
Common Reference Types
- string: Represents a sequence of characters.
- object: The base type from which all other types derive.
- dynamic: Represents a type that is resolved at runtime.
string
- Description: Represents a sequence of characters.
string name = "John Doe";
Console.WriteLine("Name: " + name);
Output:
Name: John Doe
object
- Description: The base type from which all other types derive.
object obj = 42;
Console.WriteLine("Object: " + obj);
Output:
Object: 42
dynamic
- Description: Represents a type that is resolved at runtime.
dynamic dynamicVar = 100;
Console.WriteLine("Dynamic Variable: " + dynamicVar);
dynamicVar = "Now I'm a string";
Console.WriteLine("Dynamic Variable: " + dynamicVar);
Output:
Dynamic Variable: 100
Dynamic Variable: Now I'm a string
Nullability
Reference types can hold a null value, indicating that the variable does not reference any object. Value types, by default, cannot be null, but you can use nullable value types to allow null values.