In C#.net, all data types are mainly classified in to two categories.

  • Value Type
  • Reference Type

Value Type in C#: 

A variable of a value type in C# contains its value and the variable name is directly associated with the storage location in memory where the value is stored.

Values of value types are short lived and they are generally needed for being a part of an expression. In that case, variables and temporary values often be stored on the temporary storage pool, known as stack.

For example, when we declare a value type variable as int X = 500 then the C# compiler allocates some memory space from the stack and stores the value, as shown in the image. 

ValueType
Figure – 1 – Value Type

Copy a value type to another ?

If we assign a value type variable to another value type variable, then the exact value is copied and in that case both of the variables work independently. If we change value of any variable then that doesn’t affect to the other variable.

Value Type Copy
Figure – 2 – Value Type Copy

Lets take an example and here we have two variables declared as int X = 600 and int Y = 500 , If we look at technically here then the compiler allocates memory space from stack for both variables and contains the value in it.

Value Type Assignment
Figure – 3 – Value Type Assignment

Now, when we assign variable X = Y then the compiler copies exact value of Y to X . In this case, both X and Y have value 500 stored in the stack memory.

Value Type assignment 1
Figure – 4 – Value Type assignment 1

When we reset the value of Y to 1000 compiler assigns 1000 to Y  in the stack memory. However value of X remains unchanged. Both are two different memory location. So any change to any one variable doesn’t affect the other.

Code Example :

Output:

Value Type Output
Figure – 5 – Value Type Output

Examples of value types in C#:

In C# , we have following value types available.

  • bool
  • byte
  • char
  • decimal
  • double
  • enum
  • float
  • int
  • long
  • sbyte
  • short
  • struct
  • uint
  • ulong
  • ushort

Reference Type :

Reference type holds the reference of the value, so in other words when we declare a reference type, then the compiler allocates some memory space from the managed heap and stores the value, the address of the memory is stored in the stack. 

Reference Type
Figure – 6 – Reference Type

In the above example , the variable emp is declared as a reference type, hence the compiler allocates memory from heap and stores the values. The memory address 0x00000046 of the reference type emp is stored in the thread stack.

What happens when we assign one reference type to another ?

If we assign a reference type variable to another reference type variable then the reference is copied, that means the memory address will be copied and both variables will point to same memory location. So any value change to any of the variable would affects to the other.

Reference Type
Figure – 7- Reference Type

In above example, we have declared two reference types emp and emp2 . Both these reference types have been allocated memory in managed heap.  The memory addresses of the reference types emp and emp2  have been stored in the stack as shown in the image above.

Reference Type Assignment
Figure – 8- Reference Type Assignment

Lets assign reference type emp to emp2  as follows.  emp2 = emp;  As soon as compiler sees above code, it updates the memory address of  emp2. (e.a 0x00000046 , which is memory address of emp as shown in the image).  That means, both the reference types emp and emp2 point to same memory location.

Any change to any of the variable affects the other, because they are both pointing to the same location.  The output value of emp2.Name will be “Robin”. as below

Reference Type Assignment Output
Figure – 9 – Reference Type Assignment

Code Example:

Output :

Figure - 9 - Reference Type Output
Figure – 10 – Reference Type Output

Reference Types in C#.Net :

In C#.Net , we have following reference types available.

  • class
  • interface
  • delegate
  • dynamic
  • object
  • string