Boxing and Unboxing in C#


Boxing is the implicit conversion of a value type to a reference type.


Unboxing is the explicit conversion of the reference type created by boxing, back to a value type.


Let us see an example code snippet −

// int
int myVal = 12;
// Boxing
object myBoxed = myVal;
// Unboxing
int myUnBoxed = (int) myBoxed;

Let us see another example that shows an array list in C# −

int a = 5;
ArrayList arr = new ArrayList();
// Boxing
// UnBoxing
int b = (int)arr[0];

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 19-Jun-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started