Boxing

As shown in Figure 5.1, all types are compatible with System.Object. However, because System.Object is a polymorphic type, instances in memory require an object header to support dynamic method dispatching. Value types do not have this header, nor are they necessarily allocated on the heap. The CLR allows one to use a value type (which ultimately is just memory) in contexts that use object references, such as collections or generic functions that accept System.Object as a method parameter. To support this, the CLR allows one to “clone” instances of value types onto the heap in a format that is compatible with System.Object. This procedure is known as boxing and occurs whenever an instance of a value type is assigned to an object reference ...

Get Essential .NET, Volume 1: The Common Language Runtime 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.