Information Technology Reference
In-Depth Information
The Boxing Conversions
Figure 18-24 shows the boxing conversions. Any value type ValueTypeS can be implicitly con-
verted to any of types object , System.ValueType , or InterfaceT , if ValueTypeS implements
InterfaceT .
Figure 18-24. Boxing is the implicit conversion of value types
Unboxing Conversions
Unboxing is the process of converting a boxed object back to its value type.
￿
Unboxing is an explicit conversion.
￿ The system performs the following steps when unboxing a value to ValueTypeT :
- It checks that the object being unboxed is actually a boxed value of type ValueTypeT .
- It copies the value of the object to the variable.
For example, the following code shows an example of unboxing a value.
Value type variable i is boxed and assigned to reference type variable oi .
￿
￿Variable oi is then unboxed, and its value assigned to value type variable j .
static void Main()
{
int i = 10;
Box i and assign its reference to oi.
object oi = i;
Unbox oi and assign its value to j.
int j = (int) oi;
Console.WriteLine("i: {0}, oi: {1}, j: {2}", i, oi, j);
}
Search WWH ::




Custom Search