问题描述
除了之间的定期无聊差铸造
和为
Beyond the regular boring difference between Cast
and As
- 如果我的知道的 的苹果的是水果的,所以我可以用
(水果),苹果
- 它抛出一个异常,如果它AINT -
的值
可以对空进行检查,看是否成功[不会抛出异常...]
- if i know that apple is a Fruit so I can use
(Fruit)apple
- and it throws an exception if it aint as value
can be checked against null to see if succeeded [won't throw Exception...]
不过我一直读@EricLippert article关于这一点,有一个关于空值类型一个很好的例子:
However Ive been reading @EricLippert article about this and there was a nice sample about Nullable Value Types :
short? s = (short?)123;
int? i = s as int?;
本的不会的编译...
无法将类型短?以'廉政?通过引用转换,装箱转换,取消装箱转换,包装转换或null类型转换
精细。
那么,为什么这样的:
short? s = (short?)123;
int? i = (int?)s;
请问编译? (事与愿违的!我的 KNOW 是取值
不是诠释?
- 它应该去BANG但它是不是...)
Does Compile ? ( Against ALL Expectations ! I KNOW that s
is not int?
- and it should go BANG but it aint ...)
的演员在这里检查应该更加致命高于前者的例子(其中又以邦)
the Cast checking here should be much more deadly than the former example (which went Bang)
我觉得不好问到这个备受瞩目的主题。
I feel bad asking about this much-talked subject.
在此先感谢。
推荐答案
在你的第一个例子中,为
操作员试图使用对象取值
为诠释?
。由于诠释?
是不是在任何地方的产业链短?
,此操作将失败。
In your first example, the as
operator attempts to use the object s
as an int?
. Since int?
isn't anywhere in the inheritance chain of short?
, this operation fails.
在第二个例子中,你实际上是创建一个新的诠释?我
与短的价值?小号
。这是一个更加大手笔运作,因为它不具备preserve在左侧的原始的取值
对象。
In your second example, you're actually creating a new int? i
with the value from short? s
. This is a more generous operation, because it doesn't have to preserve the original s
object on the left hand side.
这里最重要的一点是,为
是不允许做任何事情,没有preserve你的对象的身份。显式类型转换即可。
The important point here is that as
isn't allowed to do anything that doesn't preserve your object's identity. An explicit cast can.
下面就是C#的标准说关于(INT?)
的形式是如何工作的:
Here's what the C# standard says about how the (int?)
form works:
6.1.4隐式可空转换
predefined的非空值运行的隐式转换 类型也可以用于这些类型的空形式。对于每一个 在predefined隐式标识和转换数值转换 从非可空值类型S到非可空值类型T的 下列隐为空的转换中存在:
Predefined implicit conversions that operate on non-nullable value types can also be used with nullable forms of those types. For each of the predefined implicit identity and numeric conversions that convert from a non-nullable value type S to a non-nullable value type T, the following implicit nullable conversions exist:
·从S隐式转换?到T?
· An implicit conversion from S? to T?.
·从S隐式转换到T?
评估基于一个基本的隐式可空转换 从S转换到T过程如下:
Evaluation of an implicit nullable conversion based on an underlying conversion from S to T proceeds as follows:
·如果可空转换从S?以T:?
· If the nullable conversion is from S? to T?:
o如果源值为null(HasValue属性为false),该 结果是T类型的空值?
o If the source value is null (HasValue property is false), the result is the null value of type T?.
o否则,转换计算过程为选自S?解包至 S,随后从S到T的基础转换,由一 从T包装(§4.1.10)到T?
o Otherwise, the conversion is evaluated as an unwrapping from S? to S, followed by the underlying conversion from S to T, followed by a wrapping (§4.1.10) from T to T?.
·如果可空转换是从S到T ?,转换 到t接着是被评估为从S基础转换 从T包装为T?
· If the nullable conversion is from S to T?, the conversion is evaluated as the underlying conversion from S to T followed by a wrapping from T to T?.
这篇关于C#铸件可空类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!