What is unboxing in C#?


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

Let us see an example of variable and object in C# −

// int
int x = 30;

// Boxing
object obj = x;

// Un boxing
int unboxInt = (int) obj;

The following is an example showing Un boxing −

int x = 5;
ArrayList arrList = new ArrayList();

// Boxing
arrList.Add(x);

// UnBoxing
int y = (int) arrList [0];

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 22-Jun-2020

138 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements