Difference between Boxing and Unboxing in C#

CsharpProgrammingServer Side Programming

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
raja
Updated on 30-Jul-2019 22:30:23

Advertisements