Boxing and Unboxing Types

Boxing and unboxing are the processes that enable value types (e.g., integers) to be treated as reference types (objects). The value is “boxed” inside an Object, and subsequently “unboxed” back to a value type. It is this process that allowed us to call the ToString( ) method on the integer in Example 5-4.

Boxing Is Implicit

Boxing is an implicit conversion of a value type to the type Object. Boxing a value allocates an instance of Object and copies the value into the new object instance, as shown in Figure 5-4.

Boxing reference types

Figure 5-4. Boxing reference types

Boxing is implicit when you provide a value type where a reference is expected and the value is implicitly boxed. For example, if you assign a primitive type such as an integer to a variable of type Object (which is legal because int derives from Object) the value is boxed, as illustrated here:

using System;
class Boxing  
{
   public static void Main(  ) 
   {
      int i = 123;
      Console.WriteLine("The object value = {0}", i);
   }
}

Console.WriteLine( ) expects an object, not an integer. To accommodate the method, the integer type is automatically boxed by the CLR, and ToString( ) is called on the resulting object. This feature allows you to create methods that take an object as a parameter; no matter what is passed in, reference or value type, the method will work.

Unboxing Must Be Explicit

To return the boxed object back to a value ...

Get Programming C# now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.