The difference in behavior that you see is the difference between identification and presentation .
Unboxing is an identity listing and a save operation as. However, converting int to byte is a change in representation (since there is a potential loss of precision).
You get an InvalidCastException when you try to unpack an int as a byte , because the identifier of the nested value is not a byte , it is an int . When you write byte b = (byte)obj , you tell the runtime, I know that there is a byte , but that What you really want to say, I think that what can be converted to byte .
To make the last statement, you first need to declare the identifier of the object, which is int . Then and only then can you do a transformation that changes the view in byte .
Please note that this applies even if the target type is βlargerβ, i.e. Int64 . All explicit conversions for which the destination type is not in the source type inheritance tree are considered changing views. And since all types are derived from System.Object , unboxing by definition cannot change the view.
Aaronaught
source share