Monday, April 20, 2009

Boxing and Unboxing

Whenever we convert an Value type to a Reference type this process is called Boxing and opposite of that is UnBoxing

for ex :

class BoxingUnBoxing
{
static void Main()
{
int i = 1;
object o = i; // boxing
int j = (int) o; // unboxing
}
}

In this example we are assigning a value type to a reference type (eg. object o = i) , Object o is of reference type and i is of value type. and after that we assign an reference type to a value type (eg. int j = (int) o)