C# provides a "unified type system". All types – including value types – can be treated like objects. Conceptually speaking, all types derive from object, and so it is possible to call object methods on any value, even values of "primitive" types such as int
. The example
using System; class Test { static void Main() { Console.WriteLine(3.ToString()); } }
calls the object
-defined ToString
method on a constant value of type int
.
The example
class Test { static void Main() { int i = 123; object o = i; // boxing int j = (int) o; // unboxing } }
is more interesting. An int
value can be converted to object and back again to int
. This example shows both boxing and unboxing. When a variable of a value type needs to be converted to a reference type, an object box is allocated to hold the value, and the value is copied into the box. Unboxing is just the opposite. When an object box is cast back to its original value type, the value is copied out of the box and into the appropriate storage location.
This type system unification provides value types with the benefits of object-ness, and does so without introducing unnecessary overhead. For programs that don’t need int
values to act like object, int
values are simply 32 bit values. For programs that need int
’s to behave like objects, this functionality is available on-demand. This ability to treat value types as objects bridges the gap between value types and reference types that exists in most languages. For example, the NGWS class library includes a Hashtable
class that provides an Add
method that takes a Key
and a Value
.
public class Hashtable { public void Add(object Key, object Value) {...} ... }
Because C# has a unified type system, the users of the Hashtable
class can use keys and values of any type, including value types.