views:

168

answers:

3

Possible Duplicate:
Why does this conversion doesn't work?

Hi, i discovered a strange behaviour of the framework. This code throws an exception:

    byte a = 1;
    object b = a;
    Console.WriteLine(b.GetType());
    Console.WriteLine((byte)b);
    Console.WriteLine((int)(byte)b);
    Console.WriteLine(Convert.ToInt32(b));
    Console.WriteLine((int)b);

The last line throws a System.InvalidCastException.
I'd like to know what are the mechanism in the framework that make this code illegal.
Is it a problem of boxing/unboxing?!

+6  A: 

Yes, boxed value types can only be unboxed to the exact same type.

The variable b is a boxed byte.

  • When you do (int)(byte)b you're unboxing b back to a byte and then converting that unboxed byte to an int.
  • When you do (int)b you're attempting to unbox b directly to an int, which is illegal.

Edit...

As Jon mentions in his answer, there are cases where you don't have to unbox to the exact same type. Specifically:

  • A boxed T can be unboxed to Nullable<T>.
  • A boxed Nullable<T> can be unboxed to T, assuming that the nullable isn't actually null.
  • A boxed enum with an underlying type of T can be unboxed to T.
  • A boxed T can be unboxed to an enum with an underlying type of T.
LukeH
@LukeH: Not quite to the *exact* same type. See my answer for examples of unboxing to other types.
Jon Skeet
@Jon: True, but those are corner-ish cases. I didn't mention them because I was trying to keep the answer simple, but I guess it's better to be explicit about these things. I'll edit, and throw your answer an upvote too!
LukeH
+4  A: 

Eric Lippert has a blog post on this.

Why? Because a boxed T can only be unboxed to T.

Or Nullable< T >.

Yacoder
+3  A: 

When you unbox, it has to be to one of the following:

  • The exact same type
  • The nullable form of the exact same type
  • If it's an enum type value, then you can unbox to the underlying type
  • If it's an integral type value, you can unbox to an enum which uses that underlying type

Examples:

using System;

class Test
{
    enum Foo : short
    {
        Bar = 1
    }

    static void Main()
    {
        short x = 1;
        object o = x;

        short a = (short) o;
        short? b = (short?) o;
        Foo c = (Foo) o;

        o = Foo.Bar;
        short d = (short) o;
    }
}

Anything else will give an exception. In particular, you can't unbox to a different type even if there's an implicit conversion from the actual type to your target type, which is what you're trying to do on the last line of your example.

You also can't unbox from an integral value to a nullable form of an enum with the same underlying type (or the reverse situation).

Note that if you box a nullable value type value, the result is either null (if the original value was the null value for the type) or the boxed non-nullable value... there's no such thing as a "boxed nullable value type" if you see what I mean.

Jon Skeet