C# Boxing and Unboxing


This entry is part 6 of 10 in the series C# Inheritance

The post C# Introduction lists all of the C# series of posts we have at this site.

Before getting into what boxing and unboxing are, we need a quick review on value types and reference types. All types in C# are divided into two categories.

  • Value Types
  • Reference Types

Value types are stored in memory on the stack. What is the stack? When your program executes, a limited amount of memory is allocated to each thread of your program. It’s purpose is to store simple values such as all primitive types (struct, byte, bool, int, char and so on). Values stored in the stack have a short lifetime. As soon a s a value goes out of scope, the runtime kicks them off the stack and they’re gone.

Reference types are stored in the heap, not in the stack. The heap is a larger amount of memory and it is used to store objects that require a longer lifetime. Examples include any classes. The object class, the array class, the string class or any classes you write in your programs.

Boxing

Boxing is the process of converting a value type instance to an object reference. First, let’s look at ArrayList().

The first problem with ArrayList is type safety. Since it takes any object, you can have objects of different types in the same ArrayList. So, if you try to get the second element of our list shown below and try to cast it as an integer you are going to get an error. You get a runtime error InvalidCastException. The code compiles fine so you may not be aware of the error until you are testing it at runtime. The second problem is that boxing happens when we add value types to our list. That causes a performance penalty. It’s slow.

Below is a screenshot of our Generic list of strings. You can see that it takes a string.

    class Program
    {
        static void Main(string[] args)
        {
            var list = new System.Collections.ArrayList();
            list.Add(2);  // boxing happens here since 2 is value type
            list.Add("Mosh");  // no boxing since string is reference type
            list.Add(DateTime.Today);  // a structure (value type) boxing
            // var number = (int)list[1];    ERROR
            var anotherList = new List<int>();  // Generic list
            anotherList.Add(7);
            var stringList = new List<string>();
            stringList.Add("Mosh");
        }
    }
Series Navigation<< C# Upcasting and DowncastingC# Abstract Class >>