Boxing and Unboxing example for beginners

Posted by Rajni.Shekhar under C# category on | Points: 40 | Views : 1530
Boxing: copy the value of value type into object type variable.


int i=1;
object obj=i;

Here i is boxed into object obj; This is called implicit boxing.

Explicit Boxing:

int i=1;
object obj=(object) i;

Unboxing is just reverse of boxing.
"Conversion from object type to value type."

int i =0;
object obj=(object) i; //boxing value type to object type.
int x=(int) obj; //unboxing object type to value type.

Comments or Responses

Login to post response