Code to illustrate :
int i = 5;
object obj = i;
byte b = (byte)obj; // X
When run, this generates a System.InvalidCastException (“Specified cast is not valid”) at line “X”. Doing a double cast works :
byte b = (byte)(int)obj;
I would have thought that you ought to be able to cast a boxed int (if it has a value in the range 0..255) to a byte. Can anyone shed any light on this ?
(This is in .net 2.0, in case that matters).
The difference in behaviour you’re seeing is the difference between identity and representation.
Unboxing is an identity cast, and a representation-preserving operation. Casting an
intto abyte, however, is representation-changing (since there is a potential loss of precision).You get an
InvalidCastExceptionwhen you try to unbox theintas abytebecause the identity of the boxed value is not abyte, it is anint. When you writebyte b = (byte)obj, you are telling the runtime, I know that what’s in there is abyte, but what you really mean to say is, I think that what’s in there can be converted to abyte.In order to make the latter statement, you first have to declare the identity of the object, which is an
int. Then and only then can you make a representation-changing conversion tobyte.Note that this applies even if the target type is "larger" – i.e. an
Int64. All explicit conversions for which the destination type is not in the inheritance tree of the source type are considered to be representation-changing. And since all types derive fromSystem.Object, unboxing by definition cannot change the representation.