Value Type and Reference both seems to be a basic but very important part of C# programming.
Value type variables can be assigned a value directly. They are derived from the class System.ValueType. The value types directly contain data. When you declare an int type, the system allocates memory to store the value. Value Type variables are stored in the stack.
It refers to a memory location. Using multiple variables, the reference types can refer to a memory location. If the data in the memory location is changed by one of the variables, the other variable automatically reflects this change in value. Reference Type variables are stored in the heap.
S.No. | Value Type | Reference Type |
---|---|---|
1 | Value Type variables are stored in the stack | Value Type variables are stored in the heap |
2 | Stored in memory directly | Stored in memory indirectly |
3 | Default valueDepends on type (e.g. 0 for int, false for bool) | null |
4 | Examples - int, float, bool, char | Example - object, array, string, dynamic |