简体   繁体   English

C# 中的三元运算符混淆

[英]Ternary operator confusion in C#

can someone explain me how this line of C# code is calculated?有人可以解释一下这行 C# 代码是如何计算的吗?

int myInt = false? true ? 0 : 1 : 2; // ans = 2

Let's add parenthesis for the line being more readable :让我们为更具可读性的行添加括号

int myInt = false ? (true ? 0 : 1) : 2;

Now let's compute the values.现在让我们计算这些值。 For inner ?: we have对于内部?:我们有

(true ? 0 : 1)

Which equals to 0 .等于0 Then outer ?: will be然后外部?:将是

int myInt = false ? 0 : 2;

which equals to 2等于2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM