Boxing and Unboxing are concepts in C# related to the conversion between value types and reference types. These operations allow value types (like int, char, etc.) to be treated as objects, and vice versa.
Boxing is the process of converting a value type (such as int, double, char, etc.) into a reference type (specifically, an object in C#). This allows value types to be treated as objects, which is necessary when they need to be stored in collections that work with objects, like ArrayList or when passing them to methods that expect object parameters.
When a value type is boxed, the CLR (Common Language Runtime) creates a heap object to store the value, which is then wrapped inside a reference type (the object).
int num = 123; // value type (stack)
object obj = num; // Boxing: num is now a reference type (heap)
Console.WriteLine(obj); // Output: 123
In this example:
123 is a value type (stored on the stack).123 is boxed into an object, which is a reference type (stored on the heap).Boxing is an implicit operation, meaning it happens automatically when a value type is assigned to an object.
Unboxing is the process of converting a boxed object back into a value type. This requires explicit casting, as the CLR needs to retrieve the original value from the object reference.
Unboxing is a manual process and involves extracting the value from the boxed object. If the type of the object being unboxed is incompatible with the type being cast to, an InvalidCastException will occur.
object obj = 123; // Boxing happens here
int num = (int)obj; // Unboxing: explicit cast to the original value type
Console.WriteLine(num); // Output: 123
In this example:
123 was boxed into an object.int using an explicit cast (int)obj.Boxing:
object.Unboxing:
object.InvalidCastException if the types don’t match.Boxing and Unboxing in Collections:
Collections like ArrayList in earlier versions of .NET could only store object types, so value types had to be boxed to be added to the collection. However, with generic collections in modern C#, such as List<T>, boxing and unboxing are no longer required for value types, as the collection can directly store the specific type.
// Example using a generic collection (no boxing required)
List<int> numbers = new List<int>();
numbers.Add(123); // No boxing
Boxing for Polymorphism:
Boxing can be used to take advantage of polymorphism. If a value type is needed in a context where an object is required (like in some collections), boxing allows that value type to be treated as an object.
| Concept | Description |
|---|---|
| Boxing | Converting a value type to a reference type (object). It’s an implicit operation. |
| Unboxing | Converting a reference type back to its original value type. It’s an explicit operation that requires casting. |
| Performance | Boxing and unboxing incur additional overhead, which may affect performance in large-scale operations or repeated conversions. |
In general, it's important to avoid excessive boxing and unboxing in performance-critical code. Using generics in C# (like List<T>) helps avoid these operations and ensures better performance.
Open this section to load past papers