简体   繁体   English

C#条件AND(&&)OR(||)优先级

[英]C# conditional AND (&&) OR (||) precedence

We get into unnecessary coding arguments at my work all-the-time. 我们一直在工作中进入不必要的编码论证。 Today I asked if conditional AND (&&) or OR (||) had higher precedence. 今天我询问条件AND(&&)或OR(||)是否具有更高的优先级。 One of my coworkers insisted that they had the same precedence, I had doubts, so I looked it up. 我的一个同事坚持认为他们有同样的优先权,我有疑虑,所以我查了一下。

According to MSDN AND (&&) has higher precedence than OR (||). 根据MSDN AND(&&)具有比OR(||)更高的优先级。 But, can you prove it to a skeptical coworker? 但是,你能向持怀疑态度的同事证明这一点吗?

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 (||)? 所以我的问题是你如何用代码证明AND(&&)的优先级高于OR(||)? If your answer is it doesn't matter, then why is it built that way in the language? 如果你的答案无关紧要,那么为什么用这种语言建立?

Change the first false by true. 将第一个false更改为true。 I know it seems stupid to have (true || true) but it proves your point. 我知道这似乎很愚蠢(真实|| true),但它证明了你的观点。

bool result = true || true && false;   // --> true 
     result = (true || true) && false; // --> false
     result = true || (true && false); // --> true

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#, | 在C#中, | is a logical operator that performs the same boolean logic as || 是一个逻辑运算符,它执行与||相同的布尔逻辑 , but does not short-circuit. ,但不会短路。 Also in C#, the | 同样在C#中, | operator has a higher precedence than both || 运算符的优先级高于|| 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 true | false true | false is evaluated first (resulting in true ) and then that result is && ed with false to yield false . 首先计算true | false (结果为true ), 然后将结果&& ed为false以产生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 :) 被评估期间通常不会:)

Wouldn't this get you what you're after? 这不会让你得到你想要的东西吗? Or maybe I'm missing something... 或者也许我错过了一些东西......

bool result = true || false && false;

You don't prove it with code but with logic. 你不是用代码证明它,而是用逻辑证明它。 AND is boolean multiplication whereas OR is boolean addition. AND是布尔乘法,而OR是布尔加法。 Now which one has higher precedence? 现在哪个优先级更高?

false || 假|| true && true 真&&真

Yields: true 收益率:真实

false && true || false && true || true 真正

Yields: true 收益率:真实

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 || 为了更加有趣,请尝试使用result = t || t && f and see what happens with short-circuiting. t && f并看看短路会发生什么。

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

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