C# 条件 AND (&&) OR (||) 优先级
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1196703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# conditional AND (&&) OR (||) precedence
提问by Josiah Ruddell
We get into unnecessary coding arguments at my work all-the-time. Today I asked if conditional AND (&&) or OR (||) had higher precedence. One of my coworkers insisted that they had the same precedence, I had doubts, so I looked it up.
我们一直在工作中陷入不必要的编码争论。今天我问条件 AND (&&) 或 OR (||) 是否有更高的优先级。我的一个同事坚持说他们有相同的优先级,我有疑问,所以我查了一下。
According to MSDN AND (&&) has higher precedence than OR (||). But, can you prove it to a skeptical coworker?
根据 MSDN AND (&&) 比 OR (||) 具有更高的优先级。但是,你能向持怀疑态度的同事证明吗?
http://msdn.microsoft.com/en-us/library/aa691323(VS.71).aspx
http://msdn.microsoft.com/en-us/library/aa691323(VS.71).aspx
bool result = false || true && false; // --> false
// is the same result as
bool result = (false || true) && false; // --> false
// even though I know that the first statement is evaluated as
bool result = false || (true && false); // --> false
So my question is how do you prove with code that AND (&&) has a higher precedence that OR (||)? If your answer is it doesn't matter, then why is it built that way in the language?
所以我的问题是你如何用代码证明 AND (&&) 比 OR (||) 具有更高的优先级?如果你的答案是没关系,那么为什么它在语言中是这样构建的?
采纳答案by Francis B.
Change the first false by true. I know it seems stupid to have (true || true) but it proves your point.
将第一个 false 更改为 true。我知道拥有 (true || true) 似乎很愚蠢,但它证明了您的观点。
bool result = true || true && false; // --> true
result = (true || true) && false; // --> false
result = true || (true && false); // --> true
回答by Max Schmeling
Wouldn't this get you what you're after? Or maybe I'm missing something...
这不会让你得到你想要的吗?或者也许我错过了一些东西......
bool result = true || false && false;
回答by veggerby
false || true && true
假|| 真&&真
Yields: true
产量:真实
false && true || true
假 && 真 || 真的
Yields: true
产量:真实
回答by EFraim
You don't prove it with code but with logic. AND is boolean multiplication whereas OR is boolean addition. Now which one has higher precedence?
你不用代码来证明,而是用逻辑来证明。AND 是布尔乘法,而 OR 是布尔加法。现在哪个优先级更高?
回答by John Rasch
If you really want to freak him out try:
如果你真的想吓坏他,试试:
bool result = True() | False() && False();
Console.WriteLine("-----");
Console.WriteLine(result);
static bool True()
{
Console.WriteLine(true);
return true;
}
static bool False()
{
Console.WriteLine(false);
return false;
}
This will print:
这将打印:
True
False
False
-----
False
Edit:
编辑:
In response to the comment:
回应评论:
In C#, |
is a logical operator that performs the same boolean logic as ||
, but does not short-circuit. Also in C#, the |
operator has a higher precedence than both ||
and &&
.
在 C# 中,|
是一个逻辑运算符,它执行与 相同的布尔逻辑||
,但不会短路。同样在 C# 中,|
运算符的优先级高于||
and &&
。
By printing out the values, you can see that if I used the typical ||
operator, only the first True
would be printed - followed by the result of the expression which would have been True
also.
通过打印出这些值,您可以看到,如果我使用典型的||
运算符,则只会True
打印第一个- 然后是表达式的结果,该结果也将是True
。
But because of the higher precedence of |
, the true | false
is evaluated first (resulting in true
) and thenthat result is &&
ed with false
to yield false
.
但是由于 的优先级较高|
,true | false
首先评估了(导致true
),然后该结果被&&
编辑false
为 yield false
。
I wasn't trying to show the order of evaluation, just the fact that the right half of the |
was evaluated period when it normally wouldn't be :)
我不是要显示评估的顺序,只是在|
评估期间的右半部分通常不会是这一事实:)
回答by James
You cannot just show the end result when your boolean expressions are being short-circuited. Here's a snippet that settles your case.
当布尔表达式短路时,您不能只显示最终结果。这是一个可以解决您的问题的片段。
It relies on implementing & and | operators used by && and ||, as stated in MSDN 7.11 Conditional logical operators
它依赖于实现 & 和 | && 和 || 使用的运算符,如MSDN 7.11 条件逻辑运算符中所述
public static void Test()
{
B t = new B(true);
B f = new B(false);
B result = f || t && f;
Console.WriteLine("-----");
Console.WriteLine(result);
}
public class B {
bool val;
public B(bool val) { this.val = val; }
public static bool operator true(B b) { return b.val; }
public static bool operator false(B b) { return !b.val; }
public static B operator &(B lhs, B rhs) {
Console.WriteLine(lhs.ToString() + " & " + rhs.ToString());
return new B(lhs.val & rhs.val);
}
public static B operator |(B lhs, B rhs) {
Console.WriteLine(lhs.ToString() + " | " + rhs.ToString());
return new B(lhs.val | rhs.val);
}
public override string ToString() {
return val.ToString();
}
}
The output should show that && is evaluated first before ||.
输出应该显示 && 在 || 之前首先被评估。
True & False
False | False
-----
False
For extra fun, try it with result = t || t && f and see what happens with short-circuiting.
为了获得更多乐趣,请尝试使用 result = t || t && f 看看短路会发生什么。