Saturday, 31 January 2015

Boxing and Unboxing

Boxing.

In c# programming language by Boxing we mean to converting a value type to a type object or to the interface type implemented same value type. CLR wraps the value inside the system.object and store it in the garbage-collected heap. Boxing is an implicit conversion.

The example of boxing is as below:


int a = 12;
object val = a;
//The above line is boxing variable a ant its value.


Unboxing.

Unboxing is an explicit conversion from the type to a value type or from an interface type to a value type that implements the interface. An unboxing  operation is consist of checking the object instance to make sure that it is a boxed value of given value type and copying the the instance to value type variable.

The example of unboxing is as below:

int a = 12;
object val = a;    //Boxing.
int w =(int) val;       //Unboxing.

No comments:

Post a Comment