C#有条件的(安培;&安培;)OR(||)优先 [英] C# conditional AND (&&) OR (||) precedence

查看:209
本文介绍了C#有条件的(安培;&安培;)OR(||)优先的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们陷入不必要的争论编码在我的工作所有的时间。今天,我问,如果有条件的,(安培;&放大器;)或OR(||)具有更高的优先级。我的一个同事坚持认为他们有相同的优先级,我产生了怀疑,所以我看着它。

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.

根据MSDN和(安培;&放大器;)的优先级高于OR(||)。但是,你能证明给持怀疑态度的同事?

According to MSDN AND (&&) has higher precedence than OR (||). But, can you prove it to a skeptical coworker?

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



所以我的问题是,你如何证明与代码AND(安培;&放大器;)有优先级更高的OR(||)?如果你的答案是它并不重要,那么为什么它建在语言呀?

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?

推荐答案

更改第一个假通过真实的。我知道这似乎愚蠢的有(真||真),但它证明你的观点。

Change the first false by true. I know it seems stupid to have (true || true) but it proves your point.

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

这篇关于C#有条件的(安培;&安培;)OR(||)优先的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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