盒装值类型的转换
即使两个 Type
的转换有效,盒装值类型也只能拆箱到原始的 Type
中,例如:
object boxedInt = (int)1; // int boxed in an object
long unboxedInt1 = (long)boxedInt; // invalid cast
这可以通过首先拆箱到原始的 Type
来避免,例如:
long unboxedInt2 = (long)(int)boxedInt; // valid
即使两个 Type
的转换有效,盒装值类型也只能拆箱到原始的 Type
中,例如:
object boxedInt = (int)1; // int boxed in an object
long unboxedInt1 = (long)boxedInt; // invalid cast
这可以通过首先拆箱到原始的 Type
来避免,例如:
long unboxedInt2 = (long)(int)boxedInt; // valid