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 unboxingb
back to abyte
and then converting that unboxedbyte
to anint
. - When you do
(int)b
you're attempting to unboxb
directly to anint
, 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 toNullable<T>
. - A boxed
Nullable<T>
can be unboxed toT
, assuming that the nullable isn't actuallynull
. - A boxed
enum
with an underlying type ofT
can be unboxed toT
. - A boxed
T
can be unboxed to anenum
with an underlying type ofT
.
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. b
is a boxed byte
. So you need to unbox it to a byte first , like in (int)(byte)b
精彩评论