In C#, every variable has a type, which determines the size of the variable's memory, the range of values that can be stored within that memory, and the set of operations that can be applied to the variable.
Category | Data Type | Size/Range | Description |
---|---|---|---|
Numeric (Value Types) | byte | 1 byte, 0 to 255 | Unsigned 8-bit integer |
sbyte | 1 byte, -128 to 127 | Signed 8-bit integer | |
short | 2 bytes, -32,768 to 32,767 | Signed 16-bit integer | |
ushort | 2 bytes, 0 to 65,535 | Unsigned 16-bit integer | |
int | 4 bytes, -2^31 to 2^31-1 | Signed 32-bit integer | |
uint | 4 bytes, 0 to 2^32-1 | Unsigned 32-bit integer | |
long | 8 bytes, -2^63 to 2^63-1 | Signed 64-bit integer | |
ulong | 8 bytes, 0 to 2^64-1 | Unsigned 64-bit integer | |
float | 4 bytes | Single-precision floating point | |
double | 8 bytes | Double-precision floating point | |
decimal | 16 bytes | Precise decimal for financial calculations | |
Others | char | 2 bytes | Single Unicode character |
bool | 1 bytes | Boolean value (true/false) | |
Reference (Reference Types) | object | NA | Base class for all other types |
string | NA | Sequence of characters | |
class | NA | User-defined data type | |
interface | NA | Contract that classes or structs can implement | |
delegate | NA | Reference to methods with a specific signature | |
Special | nullable <T> | NA | Allows null values for value types |
dynamic | NA | Type determined at runtime | |
pointer (unsafe) | NA | Directly addresses memory locations | |
Immutable | record | NA | Immutable reference type that represents a data structure |
Value types directly store data. When you assign one value type to another, a copy of the data is made. The actual data is stored in the stack or in the location directly associated with the variable.
2.1. Basic Types Numeric Types:User-defined value types. For example:
struct Point
{
public int x, y;
}
Enumerations (or enums) are named integer constants. For example:
enum Days { Sun, Mon, Tue, Wed, Thu, Fri, Sat };
The variable doesn't directly store the data. Instead, it stores a reference (or a pointer) to the memory location where the actual data is kept, which is typically in the heap. This is what the statement "reference types store a reference to the memory location" means.
3.1. ObjectThe base class for all data types in C#. Can store any type of value.
3.2. StringRepresents a sequence of characters. For example:
string name = "John";
User-defined data structures. Classes encapsulate data for the object and methods to manipulate that data.
C# provides a special data types, the nullable types, to which you can assign normal range of values as well as null values. For example:
int? num = null;
Declared with the dynamic keyword. The type of variable declared is decided by the compiler at runtime.
dynamic val = 100;
Allows storing the memory address. Not common in C# due to safety concerns and is mostly used in unsafe programming.
Understanding data types is fundamental in C#. They determine the kind of data a variable can hold and the operations that can be performed on them. Familiarize yourself with the above types to make the best out of C# programming.