Author avatar

Joseph Gugliotta

Boxing and Unboxing Operations in C#

Joseph Gugliotta

  • Dec 18, 2019
  • 5 Min read
  • 1,372 Views
  • Dec 18, 2019
  • 5 Min read
  • 1,372 Views
Languages Frameworks and Tools
C#

Introduction

In this guide, we will learn about the operations of boxing and unboxing using the C# programming language.

By the end of this guide, you will know:

  1. What are boxing and unboxing

  2. How to box and unbox a variable

  3. What is going on during boxing and unboxing

What are Boxing and Unboxing?

The C# programming language contains three different types of data: primitive types (i.e. value types) like int and char, object types (i.e. reference types), and pointers. Boxing and unboxing deal with two of these data types, primitives and objects. Boxing is the process of converting a primitive type into an object type. You can find an example of boxing in the example code block below.

1
2
int sampleNumber = 5;  //assignment statement
Object ob = sampleNumber;  //boxing
[c#]

Unboxing, on the other hand, is the process of converting an object type into a primitive type. You can find an example of both boxing and unboxing in the example code block below.

1
2
3
int sampleNumber = 5;  //assignment statement
Object ob = sampleNumber;  //boxing
int num = (int)ob; //unboxing
[c#]

Boxing and unboxing are both important concepts in the field of object oriented programming. They enable a primitive type of data to be handled as an object, and vice versa. This is one of the building blocks of the unification of the Type System, which was implemented for the C# programming language. It allows different types of data to be used interchangeably by conversion, as long as it's a conversion that is allowed by the compiler (for example, a conversion from an int to a short is not allowed by the compiler).

What Happens During Boxing and Unboxing

Now that we know the definitions of these two concepts, we should next find out what is actually happening when you box or unbox a variable in C#.

First off, when you box a variable, you are wrapping the value into an object instance. When boxing, you are performing an implicit conversion from a primitive type to an object type. This means that when this code is run, the compiler will run an automatic type conversion. Simply put, the compiler will "promote" the value type to its reference type in order to prevent possible loss of data. An example of this would be an int being converted to an System.Int32 object by the compiler. Also, when this variable is converted to an object, it will be stored on the heap rather than on the stack with the other primitive types because since it's an object, it will need dynamic memory allocation.

When you unbox a variable, you are unwrapping an object type to a primitive type. When unboxing, you are performing an explicit conversion from an object type to a primitive type. This means that you are basically giving the compiler a specific instruction to perform this conversion because you need to tell the compiler that you wish to imply a different interpretation of this variable than the compiler would normally expect.

It is important to note that while boxing a variable, if you change the value of the original primitive variable after boxing it into an object, the newly assigned value will not persist into the new object. An example of this is in the following code block.

1
2
3
4
5
int sampleNumber = 5;  //assignment statement
Object ob = sampleNumber;  //boxing
sampleNumber = 10;
Console.WriteLine("Value of sampleNumber:  " + sampleNumber);
Console.WriteLine("Value of ob:  " + ob);
[c#]

Looking at the output, you can see that the value of ob will change in the assignment statement, but then will not change when sampleNumber is updated.

boxing example.png

Conclusion

This guide has explained boxing and unboxing of variables. We discussed examples of both and looked at the output produced, the differences between the two, and what is going on when doing each operation.

It is also worth mentioning that boxing and unboxing variables is expensive in terms of memory storage. When a variable is boxed, a new block of memory is being created and a new object is getting created and constructed. When a variable gets unboxed, it is also quite expensive for storage but to a lesser degree than boxing. Still, the cost of an unboxing operation is approximately the same as declaring a large number of regular variables. It is recommended to not perform these methods often, as continuously boxing and unboxing variables can lead to serious performance issues.

Possible job interview questions:

  1. What is boxing and unboxing?
  2. On a piece of paper or on a whiteboard, show how to box and unbox a variable.

I hope you have enjoyed reading this guide and that it will help you understand one of the most important concepts of the computer science field! If you are interested in reading my other work, check out By Value vs. by Reference: Return Values for a Function and Understanding String Immutability in C#.

9