What happens is exactly what he says.
In the first case, you have a short, unboxed, which is then explicitly assigned to the int method. This is the correct conversion that the compiler knows how to do, so it works.
In the second case, you have int, boxed, which assign an int int. This is a simple unpacking of an integer that also works, so it works.
In the third case, you have a short one in the box that you are trying to unpack into a variable that is not short. This is not a valid operation: you cannot do it in one step. This is not an unusual problem: if you use, for example, SqlDataReader , which contains a SMALLINT column, you cannot do:
int x = (int)rdr["SmallIntColumn"];
Any of the following should work in your third example:
object thirdTest = Convert.ToInt16(0); int thirdTest2 = Convert.ToInt32(thirdTest); int thirdTest3 = (int)(short)thirdTest;
Michael edenfield
source share