和装箱转换正好相反,拆箱转换是指将一个对象类型显式地转换成一个值类型,或是将一个接口类型显式地转换成一个执行该接口的值类型。 拆箱的过程分为两步: 首先,检查这个对象实例,看它是否为给定的值类型的装箱值。 然后,把这个实例的值拷贝给值类型的变量。 我们举个例子来看看将一个对象拆箱的过程。 int i = 10; object obj = i; int j = (int)obj; 这个过程用图来表示就是:
泛型相当有用,因为他们提供强制的编译时类型检查,要求更少的数据类型之间的显式转换,并减少装箱拆箱的操作和运行时类型检查。
Generics are useful because they provide stronger compile-time type checking, require fewer explicit conversions between data types, and reduce the need for boxing operations and run-time type checks.
当把一个“数据”从值类型转换成引用类型的时候,就叫做“装箱”,反之叫做“拆箱”。
When the data moves from value types to reference types its termed as 'Boxing' and the vice versa is termed as 'UnBoxing'.
应用推荐