Difference between Boxing and Unboxing in C#


Boxing convert value type to an object type whereas unboxing converts object type to the value type.

Let us see the difference between Boxing and Unboxing in C#.

Storage

In boxing, the value stored on the stack is copied to the object stored on heap memory, whereas unboxing is the opposite.

In Unboxing, the object's value stored on the heap memory is copied to the value type stored on stack.

Conversion

Unboxing has explicit conversion whereas boxing has implicit conversion.

Example

int a = 10;
object obj = a;  // boxing
int b = (int) ob; // unboxing

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Jul-2019

821 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements