|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionIn this article I will explain the concepts of Boxing and UnBoxing. C# provides us with Value types and Reference Types. Value Types are stored on the stack and Reference types are stored on the heap. The conversion of value type to reference type is known as boxing and converting reference type back to the value type is known as unboxing. Let me explain you little more about Value and Reference Types. Value TypesValue types are primitive types that are mapped directly to the FCL. Like Reference TypesReference Types are different from value types in such a way that memory is allocated to them from the heap. All the classes are of reference type. C#
ExamplesLets see some examples to have a better understanding of Value Types and Reference Types. Since we know that all ValueTypes are derived from System.ValueType r = 5;
So what do you think about the above line of code. Will it compile ? Yes it will compile. But wait what type is it cause I don't remember any type which is called You cannot write something like this since System.ValueType r = 10;
r++;
In the above example I told you that variable 'r' will be a System.ValueType r = 5;
Console.WriteLine(r.GetType()) // returns System.Int32;
Here are few samples you can try on your own:
System.ValueType r = 23.45;
Console.WriteLine(r.GetType()); // what does this print
//-------------------------------------------------------
System.ValueType r = 23.45F;
Console.WriteLine(r.GetType()); // What does this print
//-------------------------------------------------------
System.ValueType r = 2U;
Console.WriteLine(r.GetType()); // What does this print
//-------------------------------------------------------
System.ValueType r = 'c';
Console.WriteLine(r.GetType()); // What does this print
//-------------------------------------------------------
System.ValueType r = 'ac';
Console.WriteLine(r.GetType()); // tricky
//-------------------------------------------------------
System.ValueType r = "Hello World";
Console.WriteLine(r.GetType()); // tricky
Lets now jump to Boxing. Sometimes we need to convert ValueTypes to Reference Types also known as boxing. Lets see a small example below. You see in the example I wrote "implicit boxing" which means you don't need to tell the compiler that you are boxing
Int32 x = 10;
object o = x ; // Implicit boxing
Console.WriteLine("The Object o = {0}",o); // prints out 10
//-----------------------------------------------------------
Int32 x = 10;
object o = (object) x; // Explicit Boxing
Console.WriteLine("The object o = {0}",o); // prints out 10
Lets now see UnBoxing an object type back to value type. Here is a simple code that unbox an object back to
Int32 x = 5;
object o = x; // Implicit Boxing
x = o; // Implicit UnBoxing
So, you see how easy it is to box and how easy it is to unbox. The above example first boxs
Int32 x = 5;
object o = x; // Implicit Boxing
x = (Int32)o; // Explicit UnBoxing
Lets see another small example of unboxing.
Int32 x = 5; // declaring Int32
Int64 y = 0; // declaring Int64 double
object o = x; // Implicit Boxing
y = (Int64)o; // Explicit boxing to double
Console.WriteLine("y={0}",y);
This example will not work. It will compile successfully but at runtime It will generate an exception of
Int32 x = 5; // declaring Int32
Int64 y = 0; // declaring Int64 double
object o = x; // Implicit Boxing
y = (Int64)(Int32)o; // Unboxing and than casting to double
Console.WriteLine("y={0}",y);
I am sure that you all have grasp the basic understanding of Boxing and Unboxing. Happy Coding and practise a lot !
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||