Boxing

One of the core design choices made for .NET’s type system is to have a unified view over value types and reference types. By doing so, every type derives from the mother of all types: System.Object. Especially in the pregenerics era, this had several advantages because you could define a general-purpose data structure such as a list to hold objects of any type:

public class ArrayList{    private object[] items;    ...}

In languages such as Java, there was no such unification, requiring users to jump through seemingly artificial hoops to package up (the equivalent to) a value typed object in some “box” before it could be used where an Object-typed instance is expected:

// How things would look in Java, requiring ...

Get C# 5.0 Unleashed 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.