(1)概念:
Boxing and its counterpart, unboxing, enable you to convert value types to reference types and then back to value types.
Boxing装箱:value->reference
Unboxing拆箱:reference->value
(2)关联:
casting转换
(3)原理简述(装箱时):
Boxing is the term used to describe the transformation of a value type to a reference type. Basically, the runtime creates a temporary reference-type box for the object on the heap.
(4)Boxing and Unboxing codings:
1)Boxing:隐性/显性
This conversion can occur implicitly, as in the preceding example, but you can also perform it explicitly.
coding:(implicitly)
int myIntNumber = 20;
object myObject = myIntNumber;
2)Unboxing:显性
Unboxing is the term used to describe the reverse process, whereby the value of a previously boxed value type is cast back to a value type. Here we use the term cast because this has to be done explicitly.
coding:
int myIntNumber = 20;
object myObject = myIntNumber; // Box the int
int mySecondNumber = (int)myObject; // Unbox it back into an int
(5)Boxing <=>Unboxing
A variable can be unboxed only if it has been boxed. If you execute the last line when myObject is not a boxed int, you get a runtime exception thrown at runtime.