Trying to cast a boxed int to byte
问题 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). 回答1: The difference in behaviour you're seeing is the difference between