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.
- 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,
- 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.

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,
- char c1 = ‘a’;
- char c2 = ‘b’
- 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
- string str1 = “a”;
- string str2 = “b”;
- 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.
- 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.

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

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.

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’.
- static void Main(string[] args)
- {
- List<object> objList = new List<object>();
- int sum = 0;
- // Adding Name
- objList.Add("Muhammad Usama");
- // Adding Numbers
- for (int i = 1; i < 10; i++)
- {
- objList.Add(i);
- }
- // Displaying Numbers
- foreach (var o in objList)
- {
- Console.WriteLine(o);
- }
- // Casting
- for (int i = 1; i < 10; i++)
- {
- // We can't apply operations directly on object. We need to cast
- // them before use.
- // sum += objList[i] * objList[i];
- // We can't apply any numerical operation on 'object' directly
- // We need to cast it then we can apply operation on it.
- sum += (int) objList[i] * (int) objList[i];
- }
- Console.WriteLine(sum);
- Console.ReadLine();
- }
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.

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.
- int a = 20;
- 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,
- Class1 obj = new Class1();
- 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.

Hadshana KamalanathanPosted Aug 16, 2018, 1:03 AM
Thanks for sharing
Usama ShahidPosted Apr 4, 2018, 4:48 AM
Thank You So Much Guys For Your Support. You People Are Motivating Me To Keep Sharing My Knowledge :)
Satish Kumar VadlavalliPosted Apr 4, 2018, 2:01 AM
Good, keep it up :)
Naresh SinghalPosted Apr 4, 2018, 12:19 AM
Very good explained Usamabhai
Rafnas T PPosted Apr 3, 2018, 2:07 AM
Nice explanation, keep sharing..