Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Settling Casting Restrictions !!!

, 30 Jul 2010
Rate this:
Please Sign up or sign in to vote.
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:

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)

Share

About the Author

Vivek Ragunathan
Software Developer (Senior)
United States United States
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 30 Jul 2010
Article Copyright 2010 by Vivek Ragunathan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid