Mega Code Archive

 
Categories / C# Book / 01 Language Basics
 

0014 Value type vs reference type

The variables of value type store the real value, while the reference type variables store the references to the real objects. The following code defines an int type variable. int type is a value type. using System; class Program { static void Main(string[] args) { int i = 5; int j = i; i = 3; Console.WriteLine("i=" + i); Console.WriteLine("j=" + j); } } The output: i=3 j=5 From the outputs we can see that the value of j doesn't change even the value of i is changed. The value of i is copied to j when assigning the i to j.