C# – Box(ing) and Un-box(ing)

OOPS-class-object @Image courtesy of digitalart / FreeDigitalPhotos.net

C# – Box(ing) and Un-box(ing)

Welcome to CodeSpread!

I have heard this concept from many people and their different versions so I always wanted to share some insight on it which can work as a one stop solution.

What is boxing and un-boxing?

The Conversion of a value type into reference type is boxing and the reverse process is un-boxing.

What is Value Type?

Value Types in C# are the primitive data types such as Boolean, char,numeric, enum and structures.

Note: The value types are stored in stack if they are declared inside a method and are stored in heap if they are declared outside method and inside a reference type.

What is Reference Type?

Reference types are class, delegates, objects ,string and interface. A reference type is always stored in heap.

Converting Value types into reference types:

Let’s take a simple example.

int num = 5;              // num is value type

object obj = num;  //obj is reference type

Let us go through the underlying steps to understand what really happens during boxing.

Statement 1: int num = 5;

  1. The numeric constant value 5 is pushed onto stack.
  2. The value is then popped into the local variable num.


Statement 2: object obj = num ; (Box)

  1. As the value type will be represented as object, the memory is allocated on heap. The amount of memory allocated will be equal to the size of the value type plus the memory sufficient to hold object and its internal structures.
  2. The Value type (num’s) value (5) is then copied to the newly allocated heap memory.
  3. The address of the object is stored on the stack which points to newly allocated heap memory.




Converting reference types to value types:

From the above example it is clear that we don’t need to do any explicit conversion when converting value type to a reference type. However, to un-box casting is necessary. This is because, the object is being converted to a type and the run-time can validate the cast.

int num = 5; object

obj = num;

int anotherNumber = (int)obj; // Un-boxing

Now, let us understand what happens during un-boxing.

Statement 3: Un-boxing

  1. First, The runtime validates the object, pointed by the address on the stack. If the value cannot be converted to the type specified in the instruction then, InvalidCastException is thrown. If the object is valid, then CLR proceeds further.
  2. The value of the instance (in this case 5) into the value type (int) variable (anotherNumber).

.NET features automatic type handling.

Why do I need to be careful about boxing and un-boxing?

It is true that nothing special needs to be done to take advantage of boxing. However, there is some overhead associated with the process of boxing and un-boxing which affects the performance of the code.

Let’s take a look at the code below:

int num = 5;

object obj = num;

Response.Write(num + ", " + (int)obj);

How many boxing and un-boxing operations can you spot?

The correct answer is not 2 box and 1 un-box operations as it appears. Infact, 3 boxing operations and 1 un-boxing operations are being carried out in total.

  1. The first statement is merely an assignment of a numeric value to value type.
  2. As shown in the example above, the second statement is ‘Boxing’.
  3. Now, the last statement is an interesting one. The MSIL instruction which is executed to obtain the result of Write method is “String.Concat”. Now, the closest overload method is String.Concat(object,object,object)

Num (value type) is converted into object – Boxing

“,” is already a reference type. Hence no conversion is necessary.

(int)obj – The conversion has to be performed here as this is an explicit conversion –Un-boxing

Un-boxed value of (int)obj is converted back to object to be passed as a parameter to the function – Boxing

So, the explicit cast of ‘obj’ in the function costs us a needless box and un-box.

What is the difference between boxing/un-boxing and up-casting/down-casting?

Boxing/Un-boxing are transformations between value and reference types. Casting just transforms the reference types of the objects.

Boxing takes copy of value from stack to heap and un-boxing takes value type from heap to stack. While, casting does not move the objects. It merely changes the reference types of the objects.

Please share your comments and feedback here or send it to admin@codespread.com

Author: sush

Share This Post On

1 Comment

  1. nice superb explaination

    Post a Reply
  2. thanks for the tutorial
    I am having a problem.
    The feature “Create SQL server database” isn’t available , I can’t select it.
    Can you help, please ?

    Post a Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

More from CodeSpread:

  • Nullable .Net typesNullable .Net typesWe declare .net types in our project daily but still we do not recollect default values for each .net type so we end up doing null check for each data type. Its a basic mistake of a developer but ....
  • Nice reference for C# evolution Part 3Nice reference for C# evolution Part 3We completed till C# 3.0 introductions in last article and C# 2.0 in this article . We will forward our journey with C# 4.0 features. C#4.0 Late Binding (Dynamic) Late binding is always presumed...
  • My first experience with LISTS in C#My first experience with LISTS in C#Hi guys, this is Sathish, Lets discuss the concepts of lists. These are the concepts which I have planned to discuss with you guys today. What is a List and how it is advantageous than array...
  • Indexers,A Short Note.Indexers,A Short Note.What is an indexer? Indexers can be easily remembered as arrays. These arrays are special as they allow the class to be treated as arrays and access the data stored within the object. For example:...
  • Useful ‘ref’ and ‘out’ parametersUseful ‘ref’ and ‘out’ parameters It took me some time to realize the real potential of ref and out keywords, till the time I experience a condition which could only be simplified by implementing ref and out keyword. Lets st...
  • Note: GZipStreamNote: GZipStreamA short note from my experience: From the name itself you can at least make a guess about the functionality of the GZipStream class(present under: System.IO.Compression). Yes..your guess is abso...
  • Concepts: S O L I DConcepts: S O L I DSOLID - Object oriented programming principles. May be its easy to forget these principles  but for creating a reusable code or maintainable code, people will always have to come back and check ...
  • C#: Anonymous Type is not anonymous!C#: Anonymous Type is not anonymous!Why Anonymous Type? Anonymous types provide you the ease of having set of properties (Read only) into a single object without knowing the type. It’s the job of compiler to generate the type inform...