Understanding C# Data Types: Value Types, Reference Types, and Pointer Types
Explore C#'s data types—value types, reference types, and pointer types—understanding their characteristics, memory management, and how to choose the appropriate data type for your variables. This guide clarifies the differences between value and reference types and their implications for program behavior.
Understanding C# Data Types
Introduction to Data Types
In C#, a data type specifies what kind of data a variable can hold (e.g., numbers, text, true/false values). Choosing the correct data type is crucial because it impacts memory usage, the range of values a variable can store, and the operations you can perform on that data. C# has three main categories of data types.
Value Types
Value types store their data directly within the variable. When you assign a value type variable to another, a copy of the data is created. Changes to one copy don't affect the other.
Predefined Value Types
These types are built into the C# language:
- Integer Types:
sbyte
,short
,int
,long
(signed);byte
,ushort
,uint
,ulong
(unsigned) - Floating-Point Types:
float
,double
,decimal
- Character Type:
char
- Boolean Type:
bool
User-Defined Value Types
You can create your own value types using structs and enums.
Type | Size (32-bit) | Range |
---|---|---|
char |
1 byte | 0 to 65,535 |
sbyte |
1 byte | -128 to 127 |
byte |
1 byte | 0 to 255 |
short |
2 bytes | -32,768 to 32,767 |
ushort |
2 bytes | 0 to 65,535 |
int |
4 bytes | -2,147,483,648 to 2,147,483,647 |
uint |
4 bytes | 0 to 4,294,967,295 |
long |
8 bytes | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
ulong |
8 bytes | 0 to 18,446,744,073,709,551,615 |
float |
4 bytes | Approximately ±1.5 × 10−45 to ±3.4 × 1038 |
double |
8 bytes | Approximately ±5.0 × 10−324 to ±1.7 × 10308 |
decimal |
16 bytes | At least 28-29 significant digits |
Note that the memory size of some types might vary slightly depending on the system architecture (32-bit vs. 64-bit).
Reference Types
Reference types store a reference (memory address) to the data. When you assign a reference type to another variable, both variables point to the same data. Changes made through one variable are reflected in the other.
Predefined Reference Types
object
string
User-Defined Reference Types
- Classes
- Interfaces
Pointer Types
Pointer types store the memory address of a variable. They're less commonly used in C# (compared to C or C++). You use the asterisk (*) to declare a pointer. The ampersand (&) is the address-of operator; the asterisk (*) is the dereference operator.
Example Pointer Declaration
int* myIntPtr; // Pointer to an integer