为什么我不能将盒装int转换为可以为空的十进制? [重复]

时间:2023-02-07 02:57:02

This question already has an answer here:

这个问题在这里已有答案:

Why there is InvalidCastException thrown? Can someone describe me this behavior?

为什么抛出InvalidCastException?有人可以形容我这种行为吗?

object zero = 0;
decimal? dec = (decimal?)zero;

2 个解决方案

#1


A boxed int can only be unboxed to an int. This, however, is legal:

盒装int只能取消装箱到int。但是,这是合法的:

object zero = 0;
decimal? dec = (decimal?)(int)zero;

See MSDN or the ECMA 334 C# spec for details. The key here is the following:

有关详细信息,请参阅MSDN或ECMA 334 C#规范。这里的关键是以下内容:

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:

拆箱是从类型对象到值类型或从接口类型到实现接口的值类型的显式转换。拆箱操作包括:

  1. Checking the object instance to make sure that it is a boxed value of the given value type.
  2. 检查对象实例以确保它是给定值类型的盒装值。

  3. Copying the value from the instance into the value-type variable.
  4. 将实例中的值复制到value-type变量中。

Edit: This linked article is worth pulling out of the comments. Thanks Rob Kennedy!

编辑:这篇链接的文章值得退出评论。谢谢Rob Kennedy!

#2


See this article http://msdn.microsoft.com/en-us/magazine/cc301569.aspx

请参阅此文章http://msdn.microsoft.com/en-us/magazine/cc301569.aspx

Specifically "The common language runtime first ensures that the reference type variable is not null and that it refers to an object that is a boxed value of the desired value type. If either test fails, then an InvalidCastException exception is generated."

具体而言“公共语言运行库首先确保引用类型变量不为空,并且它引用的对象是所需值类型的盒装值。如果任一测试失败,则生成InvalidCastException异常。”

I think you are failing on the object of that value. I think the coversion to int works because that 0 literal will convert to an int and then an int converts to decimal.

我认为你失败了这个价值的对象。我认为对int的转换是有效的,因为0 literal将转换为int,然后int转换为decimal。

If you do this it works

如果你这样做,它的工作原理

    decimal? test=0;
    object zero = test;
    decimal? dec = (decimal?)zero;

But I think the "0" in your snippet is not a "decimal" type.

但我认为你的片段中的“0”不是“十进制”类型。

I am still not positive cause this gets the same exception.

我仍然不积极因为这会得到同样的例外。

        int test=0;
        object zero = test;
        decimal? dec = (decimal?)zero;

#1


A boxed int can only be unboxed to an int. This, however, is legal:

盒装int只能取消装箱到int。但是,这是合法的:

object zero = 0;
decimal? dec = (decimal?)(int)zero;

See MSDN or the ECMA 334 C# spec for details. The key here is the following:

有关详细信息,请参阅MSDN或ECMA 334 C#规范。这里的关键是以下内容:

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:

拆箱是从类型对象到值类型或从接口类型到实现接口的值类型的显式转换。拆箱操作包括:

  1. Checking the object instance to make sure that it is a boxed value of the given value type.
  2. 检查对象实例以确保它是给定值类型的盒装值。

  3. Copying the value from the instance into the value-type variable.
  4. 将实例中的值复制到value-type变量中。

Edit: This linked article is worth pulling out of the comments. Thanks Rob Kennedy!

编辑:这篇链接的文章值得退出评论。谢谢Rob Kennedy!

#2


See this article http://msdn.microsoft.com/en-us/magazine/cc301569.aspx

请参阅此文章http://msdn.microsoft.com/en-us/magazine/cc301569.aspx

Specifically "The common language runtime first ensures that the reference type variable is not null and that it refers to an object that is a boxed value of the desired value type. If either test fails, then an InvalidCastException exception is generated."

具体而言“公共语言运行库首先确保引用类型变量不为空,并且它引用的对象是所需值类型的盒装值。如果任一测试失败,则生成InvalidCastException异常。”

I think you are failing on the object of that value. I think the coversion to int works because that 0 literal will convert to an int and then an int converts to decimal.

我认为你失败了这个价值的对象。我认为对int的转换是有效的,因为0 literal将转换为int,然后int转换为decimal。

If you do this it works

如果你这样做,它的工作原理

    decimal? test=0;
    object zero = test;
    decimal? dec = (decimal?)zero;

But I think the "0" in your snippet is not a "decimal" type.

但我认为你的片段中的“0”不是“十进制”类型。

I am still not positive cause this gets the same exception.

我仍然不积极因为这会得到同样的例外。

        int test=0;
        object zero = test;
        decimal? dec = (decimal?)zero;