Tuesday, July 24, 2007

Boxing and UnBoxing in C#

Boxing and UnBoxing in C# :

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.

Example :
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

No comments: