Boxing refers to converting a type with a null value to a reference type or converting a value type to some interface that it implements (say int - IComparable<int> ). In addition, converting a base value type to a type with a zero value is also a box conversion. (Caution: Most discussions on this issue ignore the last two types of transformations.)
For example,
int i = 5; object o = i;
converts i to an instance of type object .
Unboxing refers to explicit conversion from an object or ValueType instance to a type with an invalid value, conversion of an interface type to a type with an invalid value (for example, IComparable<int> to int ). In addition, converting from NULL to a base type is also a conversion for unpacking. (Caution. Most discussions of this subject will ignore the last two types of transformations.)
For example,
object o = (int)5; int i = (int)o;
converts an integer placed in o to an instance of type int .
Type listing is an explicit conversion of an expression to a given type. In this way,
(type) expression
explicitly converts expression to an object of type type .
jason Jul 06 '09 at 2:29 2009-07-06 02:29
source share