开发者

C# value type casting: how it works? [duplicate]

开发者 https://www.devze.com 2023-01-15 22:01 出处:网络
This question already has an answer here: Closed 12 years ago. Possible Duplicate: Why does this c开发者_JS百科onversion doesn't work?
This question already has an answer here: Closed 12 years ago.

Possible Duplicate:

Why does this c开发者_JS百科onversion 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?!


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.


Eric Lippert has a blog post on this.

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

Or Nullable< T >.


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.


Is it a problem of boxing/unboxing?

Yes. bis a boxed byte. So you need to unbox it to a byte first , like in (int)(byte)b

0

精彩评论

暂无评论...
验证码 换一张
取 消