Boxing and Unboxing in C#

Boxing is the process of converting a value type to a reference type by wrapping it in an object. Unboxing is the reverse process ? extracting the value type from the boxed object. These operations allow value types to be treated as objects when needed.

Boxing and Unboxing Process Value Type int myVal = 12 Reference Type object myBoxed Boxing (Implicit) Unboxing (Explicit Cast) Stored on Stack Stored on Heap Boxing: Automatic | Unboxing: Requires explicit cast

Boxing

Boxing is the implicit conversion of a value type to a reference type. When boxing occurs, the value is wrapped in an object and stored on the heap instead of the stack.

Syntax

object boxedValue = valueType;  // Implicit boxing

Example

using System;

class Program {
   public static void Main() {
      // Value type
      int myVal = 12;
      
      // Boxing - implicit conversion
      object myBoxed = myVal;
      
      Console.WriteLine("Original value: " + myVal);
      Console.WriteLine("Boxed value: " + myBoxed);
      Console.WriteLine("Type of myVal: " + myVal.GetType());
      Console.WriteLine("Type of myBoxed: " + myBoxed.GetType());
   }
}

The output of the above code is −

Original value: 12
Boxed value: 12
Type of myVal: System.Int32
Type of myBoxed: System.Int32

Unboxing

Unboxing is the explicit conversion of a boxed reference type back to its original value type. This operation requires an explicit cast and can throw an exception if the types don't match.

Syntax

ValueType unboxedValue = (ValueType)boxedObject;  // Explicit cast required

Example

using System;

class Program {
   public static void Main() {
      int myVal = 25;
      
      // Boxing
      object myBoxed = myVal;
      
      // Unboxing - explicit conversion required
      int myUnBoxed = (int)myBoxed;
      
      Console.WriteLine("Original value: " + myVal);
      Console.WriteLine("After unboxing: " + myUnBoxed);
      Console.WriteLine("Values are equal: " + (myVal == myUnBoxed));
   }
}

The output of the above code is −

Original value: 25
After unboxing: 25
Values are equal: True

Using Boxing and Unboxing with Collections

Boxing and unboxing are commonly used with non-generic collections like ArrayList, which store objects −

using System;
using System.Collections;

class Program {
   public static void Main() {
      int a = 5;
      ArrayList arr = new ArrayList();
      
      // Boxing occurs when adding value type to ArrayList
      arr.Add(a);
      arr.Add(10);
      arr.Add(15);
      
      // Unboxing occurs when retrieving values
      int b = (int)arr[0];
      int c = (int)arr[1];
      int d = (int)arr[2];
      
      Console.WriteLine("Values from ArrayList:");
      Console.WriteLine("b = " + b);
      Console.WriteLine("c = " + c);
      Console.WriteLine("d = " + d);
      Console.WriteLine("Sum = " + (b + c + d));
   }
}

The output of the above code is −

Values from ArrayList:
b = 5
c = 10
d = 15
Sum = 30

Performance Considerations

Operation Performance Impact Recommendation
Boxing Memory allocation on heap Use generics to avoid unnecessary boxing
Unboxing Type checking and casting overhead Use generic collections like List<T>
Frequent boxing/unboxing Garbage collection pressure Minimize by using appropriate data types

Conclusion

Boxing converts value types to reference types implicitly, while unboxing requires explicit casting to convert back. Though useful for compatibility with object-based APIs, frequent boxing and unboxing can impact performance, so using generic collections is recommended when possible.

Updated on: 2026-03-17T07:04:35+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements