a blog for those who code

Thursday, 14 August 2014

Difference between Value Types and Reference Types

In this post we will show you what are the differences between Value Types and Reference Types.
As we all know CLR supports two kinds of types : reference types and value types. Value Types holds data within its own memory allocation whereas reference type contains a pointer to another memory location.

When you declare a variable in .NET application, it allocates a memory in the RAM. This memory has name of the variable, value of the variable and the data type of the variable. When it is a value type the compiler allocates memory in the stack and when it is a reference type, compiler creates a pointer on the stack and the actual object is stored in Heap.

So, you must be wondering why there is a difference in allocating memory between these two types. Let me give you an example,

1. int i = 0;
2. Shape sh = new shape();

When you have declared an integer type (Value Type), compiler knows how much memory it had to allocate for that variable but its not the case with reference type. Same way in the class declaration the compiler is not sure how much memory it had to allocate, since these types need dynamic memory allocation. Thus if the compiler is doing static memory allocation it will allocate the memory in stack otherwise it will allocate it in heap.

So to be concise Stack stores reference portion of reference type variables and value type variables whereas Heap stores content of reference type objects and anything which is inside a reference type object.

Some more differences between Value Types and Reference Types :-

1. Boxing and UnBoxing is applicable to Value Types. Reference types are always in a boxed form.
2. As discussed earlier Reference type variables contain the memory address instead of actual memory. So, because of this when a reference type is created, it is initialized to null. So, sometimes you might get NullReferenceException if it is not handled properly.
3. When reference type is copied to another reference type, only the memory address is copied thus the new variable will point to the existing memory address. But in case of value type field-by-field copy is made.
4. Value types are faster in access whereas as compared to value type reference type are slower to access.
5. Value types are derived from System.ValueType whereas Reference types are derived from System.Object.

Please Like and Share the Blog, if you find it interesting and helpful.

You may also like :

Casting with is and as operators in C#
Definition and Use of Indexes
Dot Net : All types are derived from System.Object
All you want to know about Common Language Run Time (CLR)
Null Coalescing operator (??) in C#
All about MongoDB Collections
How to improve Asp.Net Application Performance - Part 2
Write text files without Byte Order Mark.
Dictionary Vs Hashtable
C# NullReferenceException : Object reference not set to an instance of an object

No comments:

Post a Comment

Contact Us


Email *

Message *


Best Hosting Plans

CC License