C#: Boxing and Unboxing

MainHeader
Credits to http://www.sparefoot.com

One important aspect to learn if you want to properly handle types in C# are Boxing and Unboxing. Boxing and Unboxing are just simple to use, but sometimes creates more problems rather than being  helpful if not properly used. So lets discuss what are Boxing and Unboxing.

BOXING

Boxing in C# is the conversion of value type, e.g. (int, double) to a reference or object type. The  process includes the values are being wrapped inside the System.Object and then stores it in the heap  rather than the conventional stack for value types. Boxing is implicit by nature. Here is a sample code snippet for Boxing:

Boxing

In the example shown above the num1 which is declared as an integer type (value type) was boxed and assigned to an object type (reference type) objNum.

UNBOXING

Unboxing on the other hand is the conversion of an object type to value type. Unboxing is explicit by nature.  Here is a sample code snippet for Unboxing:

Unboxing

In the example shown above the objNum (reference type) was unboxed and was assigned to num2 (value type).

PERFORMANCE OVERHEAD

SlowPic
Credits to http://www.mindfulword.org

However Boxing and Unboxing have a performance overhead because Boxing for example, creates a new object  for the value typed to be assigned which takes up 20 times longer than a simple reference assignment.  For unboxing the casting from object to value type takes about 4 times as long as an assignment. So before you used Boxing and Unboxing you should consider also the performance overhead that is bringing in the table.

Here is a simple example for the performance overhead when using Boxing and Unboxing in your program:

SampleOverhead

The sample code snippet gets the last string and int type from an array of objects which involves unboxing of the arrayOfObjects and assigning it to the casted type. Another thing about boxing and unboxing is that you need to check the type prior to unboxing it because you will encounter InvalidCastExceptions or NullReferenceException if you accidentally casted a null value. So be careful of the pitfalls of using Boxing and Unboxing.

INVALIDCASTEXCEPTION

BoxingUnboxingInvalidCastException.PNG

From the example shown above it encountered an InvalidCastException error due to the unboxing of the value “Apple” and assigning to an integer value type.

NULLREFERENCEEXCEPTION

BoxingUnboxingNullReferenceException.PNG

From the example shown above it encountered an NullReferenceException error due to the unboxing of the null value from the arrayOfObjects.

CONCLUSION

There are pros and cons in using the Boxing and Unboxing technique in C#. Maybe you’re dealing with legacy software, pre .net 2.0 data types or 3rd party software that you cannot do away with it. It still depends on your preference or on how to solve the software design problem you’re dealing with.

HAPPY CODING 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s