Tuesday, July 5, 2011

Boxing

Boxing is an implicit conversion of a value type to a reference type. Consider the following value type variable.
           int a=12;
now boxing operation on above variable,
         object o=i; //boxing
Above statement create an object, on the stack, that references a value of type int, on the heap. this value is a copy of the vaue-type value assigned to the variable a. we ca understand it by following fig.


It also possible, but never needed to perform the boxing explicitly as in the following example:
           int a=12;
           object o= (object)  i;
  

No comments:

Post a Comment