C# - Value and Reference Types


Value Type and Reference both seems to be a basic but very important part of C# programming.

Value Type

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.

Reference Type

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
3Default valueDepends on type (e.g. 0 for int, false for bool)null
4Examples - int, float, bool, char Example - object, array, string, dynamic

Prev Next

Top Articles

  1. Why use C#
  2. How to use comment in C#
  3. How to use variables in C#
  4. How to use keywords in C#