Value Type and Reference Type

October9, 2017
by admin

There are various data types for storing integer, string, dates and custom objects. Each data type is either a value type or a reference type. It also means that each variable is either of the value type or of reference type. Let’s see what is the difference between them.

The value type variable has its own copy of the data stored in memory. So, if we make any change in the value of value type variable that will not affect the other variables.

The reference type variable doesn’t have its own copy of the data. Instead, it points to the memory address of another variable. It also means that same memory address of a variable can point or reference by one or more variables. In that case, if anyone reference type variable changes the value in the memory address to which it is pointing, then all the other reference type variables pointing to the same memory address also get updated. Let’s see this with the following example.

Ex 1:


  // value type variables
  // declaring and initializing
  int a = 5;
  int b = 10;

  // changing value of variable a
  a = 15;

  // displaying Current values
  System.Console.WriteLine(string.Format("Value of a = ", a));
  System.Console.WriteLine(string.Format("Value of b = ", b));

Output

Value of a = 15;
Value of b = 10;

In the above code example, we have declared two value type variables ‘a’ and ‘b’ with initial value 5 and 10 respectively. In the next line of code, we have changed the value of variable ‘a’ to 15. We have not changed the value of variable ‘b’. After that, we have displayed values of both the variables to the console window.

As you noticed the output, the value of variable ‘a’ is updated with new value 15, while the value of variable ‘b’ is unchanged and still store value 10.

Ex 2:


  // object type variables
  string x;
  string y;
  string firstName = "Jack";

  // referencing 
  x = firstName;
  y = firstName;

  // changing value of variable x
  x = "Bryan";

  // displaying Current values
  System.Console.WriteLine(string.Format("Value of firstName = {0}", firstName));
  System.Console.WriteLine(string.Format("Value of x = {0}", x));
  System.Console.WriteLine(string.Format("Value of y = {0}", y));

Output

Value of firstName = Bryan;
Value of x = Bryan;
Value of y = Bryan;

In the above code example, we have declared two string type variables of reference type, ’x’ and ’y’. Also, we have declared another variable ’firstName’ of type string and store value ’Jack’ in it.

In the next line of code, ’firstName’ variable is assigned to both ’x’ and ’y’. In other words, these both variables are referencing the same variable ’firstName’.

In the next line of code, we have changed the value of ’x’ to ’Bryan’. Note that we have not changed the value of variable ’y’.

After this, we have displayed the values of all three variables to the console window.

As you notice the output, since the value of ’x’ is changed to ’Bryan’, the value of ’firstName’ is changed to ’Bryan’. Why is that? That is because reference type variable ’x’ does not have its own copy of the data. It is just pointing or referencing to the memory address of variable ’firstName’. So when we changed the value of ’x’, in reality, the value of ’firstName’ is getting modified and now both the ’firstName’ and ’x’ values are same.

Also, note that the value of y is also same as the value of ’firstName’ and ’x’. That is because variable ’y’ also references to the value of variable ’firstName’ by storing the memory address of it.

Note that even though we have changed the value of one of the reference type variables ’x’, it affects the value of reference type variable ’y’ because both are referencing the same variable.

C# Fundamentals