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)

2 comments:

  1. So can i say boxing is converting value type to object type in other words.

    ReplyDelete
  2. Yes you r right rootProtect... If you need any other help just leave a comment and i revert you back..

    Cheers

    ReplyDelete