What is value type and reference type in C# .net with Example

Home ASP.Net & C#
MVC
Interview Questions & Answers
Others
What is value type and reference type in C# .net with Sample code

Introduction 

        As a dot net developer, it is very much important to understand what is happening when we are declaring a variable. For creating an application which uses memory effectively and with better performance, we need to understand how memory is allocated while we are declaring a variable.

        When we are declaring a variable in C#, it allocates some memory in RAM for that variable. There are 3 parts for this memory

1. Name of the variable
2. Value of the variable and
3. Data type of the variable.
That is it can be like below

int x = 1;







A little deeper


        This is very basic understanding. If we go a little deeper, we can see there are 2 types of memory is available for storing the variable
1. Stack     and 2. Heap
        So for now just understand - the variable you are declaring will be stored in any one of this memory, depending on the data type that you are declaring. We can skip stack and heap for now.

Now lets come to our point


What is Value type

        Variables that are stored in the stack memory are called value types. Value types contain their actual data. With value types, each variable have its own copy of data. So operations on one variable does not affect the other variable.( you can understand this point after reading reference type also ).
Consider the example below ( I suggest you to try this code yourself and get better understanding)





















The out put of the above code is









         This is one important point in value types. In line 2, I have assigned x to y and in line 3, I have updated the value of x to 5. But that is not affecting the value of y. This is because value type variables have their own copy of data. You can get better understanding while you complete reference type also.


        There is one more important thing for value types. After executing the method, the memory allocated for the value types are automatically get unallocated. That is immediately after the scope of the value type variables, memory will be cleared. They will not wait for the Garbage collector to run.

        All the built in data types of .net  excluding string and object are value types. Also, among the user defined data types, struct and enum are value types.
Example : int, long, float, double, decimal, byte, short, char, enum, struct etc.


What is reference type

       Now lets come to reference types. Unlike value type, reference type does not store the data directly.Instead they contain pointers and pointers will point to the actual data. Reference types are stored in heap memory. This can be like below














        So variable x contain the pointer to the memory location 100 and y points to 101. This is how reference types are storing the data. So, now it is possible that both x and y can point to same location say 100. Yes this is possible. See below. 














        Now what happens is, operations on one variable will affect the other variable, which is pointing to the same. That is, if x is going to change its value "Hello" to "Hello World", then what is going to happen here is it will change the value in the memory location 100 to "Hello World". After this operation if we check the value of y, we can see that value of y also got changed to "Hello World" because y is also pointing to the same location 100. 


        Therefore, 2 variables can reference a single( same ) object and operations on one variable can affect the other. We can see this change with example.























The output of the above will be 







We can see that both x and y have the same value even I have changed the value of x only. 


        One other thing we need to remember in case of reference types is it will not reclaim the memory immediately after the variable goes out of the scope. We need to wait for the garbage collector to run for unallocating the caimed memory.



Examples : string, object, class, array, delegate, interface etc. 

So we can conclude value types and reference types as



  • Value types contain the actual data while reference types will store reference to the data
  • Value types are stored in stack while reference types are stored in heap
  • Garbage collector is not required to unallocate the memory in the case of value type. But for reclaiming the reference types memory, we need wait for the Garbage collector to run. 

No comments:

Post a Comment