铸造需要使用C#三元条件运算符 [英] Required casting using C# ternary conditional operator

查看:89
本文介绍了铸造需要使用C#三元条件运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想,为什么在下面的例子中,需要强制转换弄清楚:

 布尔试验=新的随机()。 NextDouble()> = 0.5; 
短VAL = 5;

//前1 - 必须投0短
短解析度=测试? 5:0; //罚款
短解析度=测试? VAL:0; //错误
短解析度=测试? VAL:(短)0; //丑陋

//例2 - 必须转换短期或空做空?
短? NRES =测试? VAL:空; //错误
短? NRES =测试? (短?)VAL:空; //丑陋
短? NRES =测试? VAL:(短?)空; //丑陋
短? NRES =测试? VAL:(?短)违约; //丑陋



第一个例子似乎只是疯狂的给我。如果总之我= 0; 编译,为什么不能在编译器隐对待0(或任何其他有效的值)为在上面的代码?



第二个例子,使我更有意义。据我所知,编译器无法确定右侧的表达式类型 = ,但IMO这样做时,应考虑可空类型考虑在内。



我想知道,如果有这些编译器错误背后的实际理由。


解决方案

表达式测试? VAL:0 编译就好了。您在此行中得到一个错误,因为这个表达式的类型是 INT 和你试图将其分配到一个变量。这需要显式类型转换。从C#语言规范:




如果一个隐式转换(第6.1节)从X到Y存在,但不能从Y到X,则Y就是条件表达式的类型。




另一个问题是为什么,例如,文字 0 可以被分配到一个变量没有投:

 总之我= 0; 

和三元运算符的结果有定投:

 布尔测试=新的随机()NextDouble()方式> = 0.5; 
短VAL = 5;
总之我=(短)(测试VAL:0);



的原因是,第一项任务是在编译时计算的,因为它仅包含常数。在这种情况下,隐式常量表达式转换的规则:




•int类型的常量表达式(§7.19)可以转化为类型为sbyte,字节,短,USHORT,uint或ulong,则提供的常数表达式的值在目标类型的范围之内。



$ b $

 三元运算符,也可以在编译时,如果全部的操作数是常数计算>短I =真的吗? 0:int.MaxValue; 

在任何其他情况下,更严格的运行时转换规则。下面的所有3语句给编译错误:

  INT INTVAL = 0; 
短shortVal = 0;
布尔boolVal = TRUE;

短I =真的吗? 0:INTVAL;
短J =真的吗? shortVal:0;
短K = boolVal? 0:0;

有关引用您可以看到的埃里克利珀的评论



第二个例子需要治疗可空<> 作为一种特殊情况,像你已经注意到了。


I am trying to figure out why casts are required in the following examples:

bool test = new Random().NextDouble() >= 0.5;
short val = 5;

// Ex 1 - must cast 0 to short
short res = test ? 5 : 0;  // fine
short res = test ? val : 0;  // error
short res = test ? val : (short)0;  // ugly

// Ex 2 - must cast either short or null to short?
short? nres = test ? val : null;  // error
short? nres = test ? (short?)val : null;  // ugly
short? nres = test ? val : (short?)null;  // ugly
short? nres = test ? val : default(short?);  // ugly

The first example just seems crazy to me. If short i = 0; compiles, why can't the compiler implicitly treat the 0 (or any other valid short value) as a short in the above code?

The second example makes more sense to me. I understand that the compiler is unable to determine the type of the expression on the right side of the =, but IMO it should take nullable types into account when doing so.

I'd like to understand if there is actual reasoning behind these compiler errors.

解决方案

The expression test ? val : 0 compiles just fine. You get an error in this line, because the type of this expression is int and you're trying to assign it to a short variable. That requires an explicit cast. From C# language spec:

If an implicit conversion (§6.1) exists from X to Y, but not from Y to X, then Y is the type of the conditional expression.

Another question is why, for example, a literal 0 can be assigned to a short variable without a cast:

short i = 0;

And a result of ternary operator has to be cast:

bool test = new Random().NextDouble() >= 0.5;
short val = 5;
short i = (short)(test ? val : 0);

The reason is that first assignment is evaluated at compile-time, because it consists only of constants. In such case, the implicit constant expression conversion rules apply:

• A constant-expression (§7.19) of type int can be converted to type sbyte, byte, short, ushort, uint, or ulong, provided the value of the constant-expression is within the range of the destination type.

The ternary operator can also be evaluated at compile time if all operands are constants:

short i = true ? 0 : int.MaxValue;

In any other case, the stricter runtime conversion rules apply. All 3 statements below give compile errors:

int intVal = 0;
short shortVal = 0;
bool boolVal = true;

short i = true ? 0 : intVal;
short j = true ? shortVal : 0;
short k = boolVal ? 0 : 0;

For reference you can see Eric Lippert's comments.

The second example would require treating Nullable<> as a special case, like you already noticed.

这篇关于铸造需要使用C#三元条件运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆