Skip to main content

What is Boxing and Unboxing in c#?

Boxing and Unboxing

You can convert value types to reference types and back again. When a value type variable needs to be converted to a reference (object) type, an object (a box) is allocated on the managed heap to hold the value and its value is copied into the box. This process is known as boxing. Boxing can be implicit or explicit, as shown in the following code.

int p = 123;
Object box;
box = p; // Implicit boxing
box = (Object)p; // Explicit boxing with a cast


Boxing occurs most often when you pass a value type to a method that takes an Object as its parameter. When a value in an object is converted back into a value type, the value is copied out of the box and into the appropriate storage location. This process is known as unboxing.

p = (int)box; // Unboxing


Boxing issues are exacerbated in loops or when dealing with large amount of data such as large-sized collections storing value types.

To help ensure that boxing and unboxing does not significantly impact your code's performance, consider the following recommendations:

● Avoid frequent boxing and unboxing overhead.

● Measure boxing overhead.

● Use DirectCast in your Visual Basic .NET code.

Avoid Frequent Boxing and Unboxing Overhead

Boxing causes a heap allocation and a memory copy operation. To avoid boxing, do not treat value types as reference types. Avoid passing value types in method parameters that expect a reference type. Where boxing is unavoidable, to reduce the boxing overhead, box your variable once and keep an object reference to the boxed copy as long as needed, and then unbox it when you need a value type again.

int p = 123;
object box;
box = (object)p; // Explicit boxing with a cast
//use the box variable instead of p


Collections and Boxing

Collections store only data with base type as Object. Passing value types such as integers and floating point numbers to collections causes boxing. A common scenario is populating collections with data containing int or float types returned from a database. The overhead can be excessive in the case of collections due to iteration. The problem is illustrated by the following code.

ArrayList al = new ArrayList();
for (int i=0; i<1000;i++)
al.Add(i); //Implicitly boxed because Add() takes an object
int f = (int)al[0]; // The element is unboxed



To prevent this, consider using an array instead, or creating a custom collection class for your specific value type. You must perform unboxing with an explicit cast operator.

This will make it possible to write variations of the above code with no boxing.

Measure Boxing Overhead
There are several ways to measure the impact of boxing operations. You can use Performance Monitor to measure the performance impact of boxing overhead on the resource utilization and response times for your application. To do a static analysis of where exactly you are affected by boxing and unboxing in your code, you can analyze MSIL code. Search for box and unboxinstructions in MSIL by using the following command line.

Ildasm.exe Yourapplication.dll /text | findstr box
Ildasm.exe Yourapplication.dll /text | findstr unbox


exactly you optimize the boxing overhead. The overhead is significant in places where there are frequent iterations such as loops, inserting, and retrieving value types in collections. Instances where boxing occurs only once or twice are not worth optimizing.

Popular posts from this blog

iPhone Application for Islamic Duas

Islamic Duas is an app designed for Muslims around the world who don’t get the time to go through Islamic books and are looking for authentic duas to pray. This app is a solution to all your needs as every Muslim knows that a prayer in time can save you from a lot of trouble. This app carries important Islamic duas for different occasions, daily life events and end of prayer duas to seek forgiveness from Allah etc. These Islamic duas are collected from authentic sources and are very beneficial in life of a Muslim. The app is available for free for iOS and Android users. Use the following buttons to download the ap. 

Download from here:

Muslim Some of the Islamic duas included in Muslim Dua Now are: Duas for dressing/undressing/new clothesDuas for entering and leaving restroomDuas Before and After AblutionDuas for hearing AdhanDuas for Entering and leaving mosqueDua against distraction from ShaytanDuas for protection from evilDuas before and after eatingDuas to recite when fearing harm fro…

Nested Collection Models in MVC to Add Multiple Phone Numbers - Part 3

This is Part 3 of the article series. Actually, in this article series we were developing an MVC application that will allow adding multiple phone numbers using Nested Model concept. User will be able to add or remove (zero or more phone numbers, I need minimum two phone numbers for each employee) phone numbers for any single employee. Previous logs: Nested Collection Models in MVC to Add
✔ Read More...
I guess you came to this post by searching similar kind of issues in any of the search engine and hope that this resolved your problem. If you find this tips useful, just drop a line below and share the link to others and who knows they might find it useful too.

Stay tuned to my blogtwitter or facebook to read more articles, tutorials, news, tips & tricks on various technology fields. Also Subscribe to our Newsletter with your Email ID to keep you updated on latest posts. We will send newsletter to your registered email address. We will not share your email address t…

HTML5 and CSS3 Zebra Striping - Look Ma No JavaScript

It was 5 maybe 6 years ago when I first started learning jQuery. One of the first things I did was order the jQuery In Action book. If you have read that book you should remember one of the first examples given, zebra striping a table. To me this example sold me on the idea of jQuery even if I did not actually understand how jQuery worked at the time. With one line of code I could now alternate background colors in a table.
✔ Read More...
I guess you came to this post by searching similar kind of issues in any of the search engine and hope that this resolved your problem. If you find this tips useful, just drop a line below and share the link to others and who knows they might find it useful too.

Stay tuned to my blogtwitter or facebook to read more articles, tutorials, news, tips & tricks on various technology fields. Also Subscribe to our Newsletter with your Email ID to keep you updated on latest posts. We will send newsletter to your registered email address. W…