Boxing and Unboxing in C#

Boxing

Boxing means to convert any value-type to object type or to any interface type implemented by this value type. When the CLR boxes a value type, it wraps the value inside a System.Object instance. In the following example,object o is boxing the integer first.

int first=345;
object o=first;

UNBoxing

Unboxing extracts or unwraps the value-type from the object. In the following example, the object o is now unboxed and assigned to the integer variable first

o=345;
first=(int)o;

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a free website or blog at WordPress.com.

Up ↑

Design a site like this with WordPress.com
Get started