Boxing and Unboxing in C#

Boxing in C#

It means the conversion of a value type on the stack to an object type on the heap. Any type of value or reference can be assigned to an object without explicit conversion. When the compiler finds a value type where it needs a reference type. then it creates an object box into which it places the value of the value type.

int x=20;
object o1=m; // create a box to hold x

Unboxing in C#

It is the process of converting the object type back to the value type. Unboxing is an explicit conversion. When performing unboxing, C# checks that the value type is stored in the object under conversion.

int x=20;
object o1=x; // box x
float y=(int)o1; // unbox o1 back to an int