Difference Between Value Type Variables And Reference Type Variables

Before we get started between the difference of value type variables and reference type variables, we need to understand the concept of stack and heap. So, let’s get started with stack and heap.

Stack VS Heap

We’re actually working in the machine environment and we already know that the way we treat with a program is different and it executes in a different way under the hood. Normally, we assign the single value in the container and since the value is single, so we call such kind of container a static memory which holds only 1 value.

  1. int x = 123;  

 This value stores on the top of the stack.

We already know that these values are actually meaningful for us but computer understands the values in numbers and we know that the ASCII Code of ‘A’ is 65. So let’s suppose,

  1. char c = ‘A’;  

So computer understands ‘A’ itself as a number 65 under the hood. And as we can see ‘A’ is just a single value, 123 is also a single value. So, we call these containers as static containers and these variables are stored into the stack (LIFO) and they are not complex.

But the datatypes which hold more than 1 values, these datatypes are the complex datatypes. We know the architecture of Stack and heap.

Stack VS Heap Stack VS Heap

 

In Stack, variables are places one by one on the top of each other in memory. But heap is actually the dynamic memory which we use according to our need.

Let’s discuss the comparison between string and char,

As we know that string can be any length but char just holds the single character. So if,

  1. char c1 = ‘a’;  
  2. char c2 = ‘b’  
  3. char c3 = c1 + c2;  

Then we’ll see the error because char can hold just single value (ASCII number of a single value) and here, we’re using the syntax of the concatenation of string. But as the string can be any length. So, it works

  1. string str1 = “a”;  
  2. string str2 = “b”;  
  3. string str3 = str1 + str2;  

Here string can hold large values so these strings join with each other and here it works.

Why string and class are reference types?

string, object, class are actually store into the heap because they are dynamic. Let’s discuss them.

  1. string name = “Muhammad”;  

 Look we’ve already discussed that computer understands the alphabets into the numbers. And here we have 8 letters which have different ASCII numbers. So it stores the data into the heap and we’ll just pass the reference to the first letter in the stack. See the images below and you’ll get the idea what is happening under the hood.

string

 

And here these items are actual characters and they store into the memory into the ASCII numbers form.

string

 

This is how string works. As the program executes into the stack, the stack block points to the first character of the string because it has the reference to the first character’s memory reference. And then this first character has the address of the next character reference block which has the value of the 2nd character.

Similarly, in classes, we’ve multiple primitive data types and functions. So they are also complex and they store into the heap. But the object’s reference stores into the variable in a stack to point to that specific object.

Stack VS Heap

 

And if we discuss the ‘object’ we know that it is actually the dynamic type so we can store the different kind of values in it. Don’t confuse about collection/list just look what we can do with ‘object’. 

  1. static void Main(string[] args)    
  2. {    
  3.   List<object> objList = new List<object>();    
  4.   int sum = 0;    
  5.   // Adding Name    
  6.   objList.Add("Muhammad Usama");    
  7.   // Adding Numbers    
  8.   for (int i = 1; i < 10; i++)    
  9.   {    
  10.     objList.Add(i);    
  11.   }    
  12.   // Displaying Numbers    
  13.   foreach (var o in objList)    
  14.   {    
  15.     Console.WriteLine(o);    
  16.   }    
  17.   // Casting    
  18.   for (int i = 1; i < 10; i++)    
  19.   {    
  20.     // We can't apply operations directly on object. We need to cast    
  21.     // them before use.    
  22.     // sum += objList[i] * objList[i];    
  23.   
  24.     // We can't apply any numerical operation on 'object' directly    
  25.     // We need to cast it then we can apply operation on it.    
  26.     sum += (int) objList[i] * (int) objList[i];    
  27.   }    
  28.   Console.WriteLine(sum);    
  29.   Console.ReadLine();    
  30. }    
As we can see, we can store different type of primitive values into the ‘object’ because it is dynamic memory. We can allocate anything into the dynamic container. Another noticeable thing is we can’t apply the operations directly on the object container because it can’t be possible to apply the numerical operations on the dynamic memory container until it needs a lots of customization or coding. So we need to cast the ‘object’ container first into a specific type to apply the operations upon it which we can apply on that specific container.

If we’re casting ‘object’ into string then we can concatenate it, if we’re casting it into numerical container then we can apply the numerical operations upon it. So conclusion is, the operations which we want to apply is dependent upon the casting. Here is the output of the above program.



Value Type and Reference Type

The built-in types are also known as value types because variables store the actual data. But reference types just store the reference of that specific object.

Let’s discuss value type and reference type with some other dimension.

  1. int a = 20;  
  2. int b = a;  

When we assign the value type to another value type then the new copy of container ‘b’ will be generated. Because ‘a’ has value and now this value is also assigned to another variable ‘b’. But in reference type case,

  1. Class1 obj = new Class1();  
  2. Class1 obj1 = obj;  

Here obj has the reference to the object which places into the heap and now this reference is copied into the obj1 as well. Now both variables are pointing to the same memory location. This is how reference type works.


Recommended Free Ebook
Similar Articles