简体   繁体   English

C#中的(true && false || true)是什么意思?

[英]What is the meaning of (true && false || true) in C#?

If I have this equation: 如果我有这个等式:

    var x = (true && false || true)

Is that equivalent to: 这相当于:

    var x = ((true && false) || true)

or: 要么:

    var x = (true && (false || true))

And whats the logic behind this? 这背后的逻辑是什么?

AND wins over OR. AND胜过OR。

So it will be 所以它会

var x = ((true && false) || true)

See Operator precedence and associativity . 请参阅运算符优先级和关联性

In boolean logic, "not" ( ! ) is evaluated before "and" ( && ) and "and" is evaluated before "or" ( || ). 在布尔逻辑中,在“和”( && )之前评估“不”( ! ),在“或”( || )之前评估“和”。 By using the double ( && ) and the double ( || ), these operators will short circuit, which does not affect the logical outcome but it causes terms on the right hand side to not be evaluated if needed. 通过使用double( && )和double( || ),这些运算符将短路,这不会影响逻辑结果,但如果需要,它会导致右侧的术语无法进行评估。

Thus 从而

var x = (true && false || true) evaluates to false|| true var x = (true && false || true)计算结果为false|| true false|| true which evaluates to true false|| true ,其值为true

and

var x = ((true && false) || true) evaluates to false || true var x = ((true && false) || true)求值为false || true false || true which evaluates to true false || true ,其值为true

and

var x = (true && (false || true)) evaluates to true && true which evaluates to true var x = (true && (false || true))计算结果为true && true ,其值为true

I believe it's 我相信它

var x = ((true && false) || true)

...as && has precedence according to MSDN. ...根据MSDN, &&具有优先权

You'd think that whoever wrote that particular line of code might have made their intention clear by inserting the parenthesis in the right place. 您认为编写该特定代码行的人可能通过在正确的位置插入括号来明确其意图。 Do everyone else a favour, and add them in. 帮助其他人,并将其添加进去。

It's equivalent to 它相当于

var x = ((true && false) || true)

The && operator has higher precedence than the || &&运算符的优先级高于|| operator. 运营商。

Operator precedence MSDN documentation 运算符优先级MSDN文档

&& has higher precedence, so the second form (of the three) is correct. &&具有更高的优先级,因此第二种形式(三种)是正确的。 As to the logic, && tends to be vaguely associated with multiplication, and || 至于逻辑, &&往往与乘法和||模糊地联系在一起 with addition (if you use zero and non-zero to represent false and true, respectively, the operators have equivalent semantics). 添加(如果使用零和非零分别表示false和true,则运算符具有等效的语义)。 But mostly it's just the way it's been since C, and possibly before C. 但主要是它自C以来的方式,可能在C之前。

If you use in this expression || 如果你在这个表达式中使用|| true that mean doesn't matter about other result it always will be true 是的,这意味着与其他结果无关,它总是如此

The above expression evaluates as ((true && false) || true) because of the operator precedence(&& has higher precedence than ||). 由于运算符优先级(&&的优先级高于||),上面的表达式求值为((true && false) || true) )。

Check this link for more information on Operator Precedence: http://msdn.microsoft.com/en-us/library/aa691323(v=vs.71).aspx 有关运算符优先级的更多信息,请查看此链接: http//msdn.microsoft.com/en-us/library/aa691323( v = vs.71).aspx

&& has a higher precedence than || &&的优先级高于|| so the first two use cases will act exactly the same. 所以前两个用例的行为完全相同。 The braces have no meaning here, just like in: 大括号在这里没有任何意义,就像在:

a * b + c = (a * b) + c a * b + c =(a * b)+ c

You can control precedence and associativity with braces. 您可以使用大括号控制优先级和关联性。 so the third use case will check the OR condition and then the AND. 所以第三个用例将检查OR条件然后检查AND。

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

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