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 program is different and it executes in different way under the hood. Normally we assign the single value in the container as the value is single so we call such kind of container is 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 store into the stack (LIFO) and they are not complex.
But the datatypes which holds more than 1 value, these datatypes are complex datatype. 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 lengthy 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 single value) and here we’re using the syntax of 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 string join with each other and here it works.
Why String and Class Are Reference Type?
string, object, arrays, 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 understand 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 of the first letter in stack. See the images below and you’ll get the idea what is happening under the hood.
And here these items are actually characters and they store into the memory into the ascii numbers form.
This is how string works. As program executes into the stack and stack block points to the first character of the string because it has the reference of 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 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();
}
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.
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 of the object which places into the heap and now this reference is copies into the obj1 as well. Now both variables are pointing to the same memory location. This is how reference type works.
0 comments:
Post a Comment