Boxing and Unboxing in .NET
Published: 07 Sep 2006
The article deals with the basic idea of Boxing and Unboxing techniques. Though handled automatically by the Common Language Runtime, it is useful for developers to have an understanding of how these processes affect their programs.
by Sandeep Acharya
Average Rating: 
Views (Total / Last 10 Days): 49550/ 58


This article deals with the boxing and unboxing in Microsoft .NET. It is a fact that in our daily coding standards we usually forget to keep track of these things, but each developer should know what it means and that the proper use of this will definitely lead to a powerful application.

Before going into Boxing and Unboxing, it is really very important to understand what a primitive data type is. All the supported datatypes by the compiler of a language are known as primitive data types. These data types map directly to the library of the corresponding language. For example, in C# we are declaring an integer variable as

Listing 1

int a = new int (5);

Or in a friendlier format:

Listing 2

int a = 5

Both listing 1 and 2 are same.

The int data type is a primitive data type of C# and for that reason it maps directly to System.Int32 type. So the above two lines can be re-written as:

Listing 3

System.Int32 a = new System.Int32 (5) 
System.Int32 a = 5
Reference and Value types

Now let us get an idea regarding reference type and value type objects. We all know that a new operator returns the memory address of an object that is allocated from the managed memory (a pool of memory). We usually store this address in a variable for our convenience. These types of variables are known as reference variables. However, the value type variables are not supposed to have the references; they always used to have the object itself and not the reference to it.

Now if we consider the statement written above in Listing 3, the C# compiler will detect System.Int32 as a value type and the object is not allocated from the memory heap, assuming this as an unmanaged one.

In a general way we should declare a type as a value type if the following are true.

1.      The type should be a primitive type.

2.      The type does not need to be inherited from any other types available.

3.      No other types should also be derived from it.

A few other criteria are also there for this, but are beyond the scope of this article.

Few differences between value and reference

Value type objects have two representations: an unboxed form and a boxed form. Reference types are always in a boxed form.

Value types are implicitly derived from System.ValueType. This type offers the same methods as defined by System.Object.

Reference type variables contain the memory address of objects in the memory heap. By default, when a reference type variable is created it is initialized to null, indicating that the reference type variable does not currently point anywhere. So whenever we try to access a null referenced variable, we get NullReferenceException exception. Whereas a value type variable always has a value. By default, all members of the value type are initialized to zero. It is not possible to generate a NullReferenceException exception when accessing a value type.

While copying a value type to another value type, the value only gets copied. However, assigning a reference type variable to another reference type variable causes only the copy of the memory address.

Because of the previous point, it may be possible that two or more reference type variables can refer to the same object in the managed heap (pool of memory). This means an operation on one variable may hamper the object referenced by another variable. On the other hand, value type variables each have their own copy of the object's data and it is not possible for operations on one value type variable to affect another.

What is Boxing and UnBoxing?

Boxing and Unboxing are two important portions of .NET technology. Many of the developers are not really aware of it, but it is really needed if somebody is interested in enhancing the performance of the application developed.

In a very brief way, one can say that Boxing is nothing but converting a value type object to a reference type object. Unboxing is completely explicit. The idea will be clearer after discussing the example given below. This example shows how an integer type is converted to a reference type using the boxing and then unboxed from the object type to the integer type.

Listing 4

class Test
  static void Main()
    int i = 1; // i is an integer. It is a value type variable.
    object o = i;
      // boxing is happening. The integer type is parsed to //object type
    int j = (int)o;
      // unboxing is happening. The object type is unboxed to //the value type


In the above example, it is shown how an int value can be converted to an object and back again to an int. This example shows both boxing and unboxing. When a variable of a value type needs to be converted to a reference type, an object box is allocated to hold the value and the value is copied into the box.

Unboxing is just the opposite. When an object box is cast back to its original value type, the value is copied out of the box and into the appropriate storage location.

Make it inside out

As we already discussed, the converting of a value type to a reference type is known as Boxing. Now let us see what actually happens behind the screen during Boxing.

The memory is allocated from the managed memory (can be termed as heap also). The amount of memory allocated = the size of Value type + some overhead memory. This overhead memory is required as it includes pointer to virtual method table and pointer to sync table.

Now once the memory is allocated, the inside value of the value type variable gets copied to the newly allotted location.

Then the reference to the memory block is returned.

Figure 1


Key Terms

Type Info Block (TIB)

This block is typically an array of pointers to all the objects of a class.  It also holds much of the other information for a class.

Virtual method table (VMT)

This table holds the address of instruction that had asked for the value.  Now let us have a look at what actually happens during the Unboxing time.

Here, the .NET CLR first ensures that the reference type variable is not Null and is in a valid boxed format.

If the types do match, then a pointer to the value type contained inside the object is returned. The value type that this pointer refers to does not include the usual overhead associated with a true object (i.e. a pointer to a virtual method table and a sync block).

In a nutshell we can say that Boxing always creates the new object and copies the unboxed value to it while unboxing gives a pointer.  This pointer is nothing but points to the data in the boxed object.


Both boxing and unboxing are wanted devils. While the boxing process occurs, there is an additional memory loss occurrence. This memory is actually needed for pointer to virtual method and pointer to sync table which is not there in the case of value type objects.

During the unboxing process, however, memory relaxation occurs, but at the same time some unwanted checking (like whether the reference type variable is not Null or it is a valid boxed value or not) takes place.

If we compare Reference type and Value type Objects, then we certainly prefer Value Type objects. This is because as Reference types are coming from the managed heap/memory, they used to affect the Garbage Collection and memory management. On other hand, since Value types run on thread’s stack there is no need to be worried for a garbage collection to occur for them.

So, for these reasons we should avoid unnecessary boxing/unboxing processes and only add them when the application demands it. However, the fact remains that most of the developers are not aware of the underlying complications and they repeat these unnecessary processes unknowingly, which can lead to the degradation of the performance.


The above topic is an important issue that should be known to each .NET developer. One must understand what a reference type and value type variable are and what impact they have on the application developed. The C# and Visual Basic compiler automatically generate boxing and unboxing code. This makes programming easier and at the same time leaves the user free from the performance issue. But unlike these languages, there are few other languages that force explicit Boxing/Unboxing. So, it is a good practice to go behind the screen.

User Comments

Title: nfl jerseys cheap   
Name: NIKE NFL jerseys
Date: 2012-07-02 10:09:49 AM
We are professional jerseys manufacturer from china, nike nfl jerseys, mlb jerseys, nhl jerseys,nba jerseys and shoes
,heap jerseys,2012 nike nfl Jerseys,nba jersey and shorts,oklahoma city thunder jersey,official jeremy lin new york knicks jersey,NFL Jerseys Wholesale,blake griffin jersey blue,NFL jerseys For Sale online.All Our Jerseys Are Sewn On and Directly From Chinese Jerseys Factory
,Wholesale cheap jerseys,Cheap mlb jerseys,]Nike NFL Jerseys,Cheap China Wholesae,Wholesale jerseys From China,2012 nike nfl Jerseys,Jerseys From China,,2012 nike nfl Jerseys,Revolution 30 nba jerseys,jersey of nba chicago bulls direk rose ,nfl jerseys,green bay packers jerseys wholesale,Buffalo Bills nike nfl jerseys sale,good supplier soccer jerseys,cool base mlb jerseys,Revolution 30 nba jerseys,2012 stanley cup nhl jersey,
We are professional jerseys manufacturer from china, nike nfl jerseys, mlb jerseys, nhl jerseys,nba jerseys and shoes.
Title: what is boxing and unboxing?   
Name: sim
Date: 2011-07-26 12:48:00 PM
ya i understud now!!.....
Title: boxing & unboxing   
Name: dharmendra kunar
Date: 2011-04-07 8:58:09 PM
i thing here the complete disscus about boxing and unboxing
Title: Web Developer   
Name: Dali
Date: 2011-02-20 3:46:16 PM
Thank you much. Keep up your good work.
Title: boxing and un boxing   
Name: Eknath Dohale
Date: 2010-10-07 8:48:07 AM
Nice article... Keep it up...!
Title: boxing and un boxing   
Name: Vikas Kumar
Date: 2010-09-19 4:39:42 AM
Good Explanation
Title: boxing and un boxing   
Name: shankar
Date: 2010-09-14 2:02:03 AM
gr8 work
Title: boxing &unboxing   
Name: lakshmanaro
Date: 2010-07-28 3:28:54 AM
what is boxing why are the boxing
Title: need more sample   
Name: walkincg
Date: 2010-07-24 9:26:54 AM
we have the value like below
int i = 2;
string str = i.ToString() // is there boxing is performing
int j = 1; //is there unboxing is performing
Title: What is Boxing and UnBoxing?   
Name: vinay h
Date: 2010-05-27 5:34:14 AM
Good description..very usefull and easy to understand.
Title: What is Boxing and UnBoxing?   
Name: Sridhar.R
Date: 2010-05-11 2:50:47 AM

Nice One. and very useful for as u said for being a developer.
Title: Boxing   
Name: Rohan
Date: 2010-02-16 7:22:44 AM
nice example..each and every can easily understud..
Title: need more   
Name: nadhiya.s
Date: 2009-08-14 12:04:56 PM
nice example.. but i need more example program ya...
Title: Thanks Shakuntala   
Name: Sandeep Acharya
Date: 2008-08-14 9:42:17 PM

Thanks for your posting....
But this one seems to be an invalid casting...If it could be a valid conversion then yes it would be an unboxing.
Title: Need clarification on Ex.of Boxing and Unboxing   
Name: Shakuntala
Date: 2008-08-14 3:28:28 AM
String is a reference type so
if we declare
string name = "Shalini";
int i = Int.Parse(name);(here we are converting refernce type to int type so it is unboxing)
is this correct one??
Title: Boxing and UnBoxing   
Name: Giribabu
Date: 2008-07-11 1:16:55 AM
Everyone is using same example, can you tell/ give me real time example's so that anyone can explain to an interviewer who is asking this question. i hope he will not satisfy by simple explaining this concept as it is. I will give you a sample where we use boxing in realtime project, you just explain me about that.

Suppose MyTestProject Entity has productID
MyTestProject obj = new MyTestProject();
Session["ProductID"] = obj.productID;

can you explain it?
Title: Boxing & UNBoxing   
Name: prasad
Date: 2008-05-21 5:36:37 AM
kinly give me more examples about it and what is the advantages of using boxing&Unboxing.How should explain in the interview.

Note:please write the definition
Title: Boxing & UNBoxing   
Name: venkat
Date: 2008-04-09 4:01:28 AM
give me more examples & what is the benifit
send me
Title: Boxing & UNBoxing   
Name: Shashi
Date: 2008-03-23 4:36:22 AM
give me some more examples,
and tell it benefits,Where we r using..

please give me clear idea about these Article. mail me
Title: Real Boxing   
Name: Satish Dasari
Date: 2008-03-05 11:52:23 AM
Title: Clarity on the Basic   
Name: B Srinivasan
Date: 2007-12-27 1:11:15 PM
Good description with the sample to explain the Boxing concept. Its worth for the beginner/new to this .NET feature
Title: Boxing & Unboxing   
Name: saleem
Date: 2007-12-20 3:41:19 PM
-give me some more examples,
and tell it benefits.
-Why we are using boxing and unboxing. what is the use. where
we are using this concept. can u say.

please give me clear idea about these Article. mail me @ (
Title: Good Job   
Name: Harry
Date: 2007-12-05 6:58:43 AM
Nice example. ;-)
Keep writing.
Title: use less article   
Name: sameer
Date: 2007-10-09 8:44:05 AM
useless article doenst make sense to me at all
Title: Keep up the good work   
Name: Rajasekhar
Date: 2007-08-22 11:34:44 AM

This is the correct way to explain any concept. It's really outstanding for us. Please keep writing the articles like this.

Regards - Rajasekhar.
Title: Best explanation online about this subject   
Name: Hung Dao
Date: 2007-07-23 5:53:40 PM
I have read several online examples. All simply talks about how boxing and unboxing work but do not really show behind the scene. Keep it up your great work.
Title: nice very good examples are given But   
Name: suyog kale(
Date: 2007-06-08 5:36:24 AM
give me some more examples,
and tell it benefits
Title: Boxing   
Name: shiva
Date: 2007-05-31 3:24:33 AM
nice very good examples are given
Title: boxing   
Name: velavan
Date: 2007-04-19 7:03:44 AM
very good
Title: About boxing and unboxing   
Name: sathyanarayanan. c
Date: 2007-03-24 7:30:47 PM
Why we are using boxing and unboxing. what is the use. where we are using this concept. can u say.
Title: Nice   
Name: Bhupender
Date: 2007-03-22 6:21:45 AM
Its Nice, But want some more examples ...
Title: Nice   
Name: Meenu
Date: 2007-02-19 5:44:16 AM
Its really nice..Easily understandable..
Well done..
Title: Boxing when calling methods   
Name: ad
Date: 2007-02-09 8:20:36 AM
Suppose we have three methods:
- Boxing::CheckBoxingInt(int32)
- Boxing::CheckBoxingObject(object)
- Boxing::CheckBoxingValueType(ValueType)

Boxing (see *) is done when calling CheckBoxingObject() and CheckBoxingValueType(), but not when calling CheckBoxingInt()...

Is this correct, or?

call instance void Boxing::CheckBoxingInt(int32)

* box [mscorlib]System.Int32
call instance void Boxing::CheckBoxingObject(object)

* box [mscorlib]System.Int32
call instance void Boxing::CheckBoxingValueType(class [mscorlib]System.ValueType)
Title: Fine Article   
Name: sonj
Date: 2007-02-08 10:04:03 AM
Thanks for directing the Developers
Title: Better method   
Name: prashant kumar singh
Date: 2007-02-04 9:31:19 AM
this site is providing better method to uderstanding any topic.
Title: Very Nice   
Name: kiran
Date: 2006-12-22 2:40:58 PM
Please try some more......Help
Title: useful article   
Name: keert
Date: 2006-12-15 5:35:52 AM
very useful
Title: Good one   
Name: prince
Date: 2006-10-18 7:27:40 PM
This information helps me alot,thanks
Title: I Love You Author   
Name: I Love You Author
Date: 2006-10-11 12:45:00 AM
I Love You Author.
Title: Thanx   
Name: mansur saeed
Date: 2006-10-10 3:06:41 PM
think you very much &carry on
Title: I love it   
Name: .NET Pro
Date: 2006-09-13 9:40:45 PM
I love this article, good advice.
Title: Excellent   
Name: Kishor
Date: 2006-09-13 2:26:03 AM
Really very usefull article. Keep it up
Title: Nice one   
Name: Sarith koottalakkal
Date: 2006-09-11 10:55:39 PM
Keep it up man..
Title: well done   
Name: udit
Date: 2006-09-09 3:43:47 AM
the information is given by u regarding boxing & unboxing
it cleare my doubt thanks.keep in touch.
Title: Good Example   
Name: Anoop
Date: 2006-09-09 3:43:17 AM
Good Example keep it up
Title: Keep it up   
Name: Manish
Date: 2006-09-08 3:21:08 AM
Good, Carry on with ur work
Title: Well Done   
Name: Anita Acharya
Date: 2006-09-07 11:50:20 PM
U have chosen a new hobby in ur life.Best of luck & God Bless U.
Title: Keep it up   
Name: prashant
Date: 2006-09-07 9:14:58 AM
Carry on with u r work.

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2023  |  Page Processed at 2023-11-29 9:23:57 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search