Click here to Skip to main content
15,886,362 members
Articles / Programming Languages / C#

Settling Casting Restrictions !!!

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
30 Jul 2010CPOL 5.3K   1  
Remember the Casting Restrictions we discussed a while back, let us settle that now.

Remember the Casting Restrictions we discussed a while back, let us settle that now. So we have some code like this:

C#
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 (
    [0] int32 i,
    [1] object obj,
    [2] int64 l)
L_0000: nop
L_0001: ldc.i4.s 100
L_0003: stloc.0
L_0004: ldloc.0
L_0005: box int32
L_000a: stloc.1
L_000b: ldloc.1
L_000c: unbox.any int64
L_0011: stloc.2
L_0012: ret

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.

From MSDN

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 unbox was meant to be like that.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --