Difference between a Value Type and a Reference Type in .Net
DotNet Framework is an Object Oriented programming platform. Memory allocation for data members of a program in .net is divided in two types Value type and References type. As the name suggests Value types store data directly in them where as reference types keep a reference of the data. Every object created in .net is derived from System.Object class whether its value type or a reference type.
Now, this being a fact how can we differentiate between these two type and what was the need of two different types. In order to understand these we need to look in to where DotNet creates variables. DotNet uses two data structures Stack and Heap to store data members. Every declaration whether its a value type or a reference type is stored in stack.
int _i;
System.Data.DataSet objSet;
Above declarations are stored in stack, but the integer variable will also stores its value on stack and the DataSet variable will keep a reference of dataset obj. Actual Data Set object is created in heap. Value types are generally small in size so they are kept directly on stack, whereas reference can be huge so they managed differently, this gives performance advantage to compiler.
Also each value type will be derived from System.ValueType which again is derived from System.Object.
All basic data types like Integers, Long, Float, Decimal, Char and Double etc are value types in .Net, beside these every structure and enumeration is a value type.
How to determine whether an object is value type or reference type?
Code Sample -
Dim objset as new System.Data.DataSet();
if objset.GetType().IsValueType then
MessageBox.Show("Value Types")
else
MessageBox.Show("Reference Types")
End If
Now, this being a fact how can we differentiate between these two type and what was the need of two different types. In order to understand these we need to look in to where DotNet creates variables. DotNet uses two data structures Stack and Heap to store data members. Every declaration whether its a value type or a reference type is stored in stack.
int _i;
System.Data.DataSet objSet;
Above declarations are stored in stack, but the integer variable will also stores its value on stack and the DataSet variable will keep a reference of dataset obj. Actual Data Set object is created in heap. Value types are generally small in size so they are kept directly on stack, whereas reference can be huge so they managed differently, this gives performance advantage to compiler.
Also each value type will be derived from System.ValueType which again is derived from System.Object.
All basic data types like Integers, Long, Float, Decimal, Char and Double etc are value types in .Net, beside these every structure and enumeration is a value type.
How to determine whether an object is value type or reference type?
Code Sample -
Dim objset as new System.Data.DataSet();
if objset.GetType().IsValueType then
MessageBox.Show("Value Types")
else
MessageBox.Show("Reference Types")
End If
Labels: .net


1 Comments:
http://blogs.msdn.com/ericlippert/archive/2009/02/17/references-are-not-addresses.aspx
Readers who are interested in reference types and their details please do visit the link above. It has got some very cool information about references
Post a Comment
Subscribe to Post Comments [Atom]
<< Home