Boxed value types can only be unboxed into their original Type
, even if a conversion of the two Type
s is valid, e.g.:
object boxedInt = (int)1; // int boxed in an object
long unboxedInt1 = (long)boxedInt; // invalid cast
This can be avoided by first unboxing into the original Type
, e.g.:
long unboxedInt2 = (long)(int)boxedInt; // valid