Remember the Casting Restrictions we discussed a while back, let us settle that now. So we have some code like this:
int i = 100;
object obj = i;
long l = (long)obj;
And an invalid cast exception while casting ‘obj’ to long. It is obvious that we are not changing the value held by obj, but just reading it. Then why restrict such casting. Let us disassemble and see what we got.
.locals init (
 int32 i,
 object obj,
 int64 l)
L_0001: ldc.i4.s 100
L_0005: box int32
L_000c: unbox.any int64
Oh, there we see something interesting – unbox. So the C# compiler uses the unbox instruction to retrieve the value from obj while casting; it does not use Convert.ToInt64 or similar mechanism. That is why the exception was thrown.
Unboxing is an explicit conversion from the type object to a value type or from an interface type to a value type that implements the interface. An unboxing operation consists of:
- Checking the object instance to make sure it is a boxed value of the given value type
- Copying the value from the instance into the value-type variable
So we are blown at step 1 of the unbox operation. Let us play with what we have for now, and stop bugging why was unbox meant to be like that.