是什么逻辑和条件AND,OR在C#中的区别? [英] What is the difference between logical and conditional AND, OR in C#?

查看:284
本文介绍了是什么逻辑和条件AND,OR在C#中的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/35301/what-is-the-diffference-between-the-and-or-operators\">What是的diffference |和||还是运营商?

逻辑AND和OR:

(x & y)
(x | y)

条件AND和OR:

Conditional AND and OR:

(x && y)
(x || y)

我只知道关于有条件的操作数到这一点。我知道它做什么,以及如何在应用它if语句。但是,什么是逻辑运算的目的是什么?

I've only known about conditional operands up to this point. I know what it does and how to apply it in if-statements. But what is the purpose of logical operands?

推荐答案

我preFER认为它是按位与有条件的,而不是逻辑VS有条件的自逻辑的一般概念适用于这两种情况。

I prefer to think of it as "bitwise vs. conditional" rather than "logical vs conditional" since the general concept of "logical" applies in both cases.

x & y    // bitwise AND, 0101 & 0011 = 0001
x | y    // bitwise OR,  0101 | 0011 = 0111

x && y   // true if both x and y are true
x || y   // true if either x or y are true

修改

应广大用户要求,我也应该提到的是参数不同的评价。在有条件的版本,如果整个操作的结果可以由第一参数来确定的,第二个参数不评估。这就是所谓的短路的评价。位运算有为了计算最终值来评价两侧。

By popular demand, I should also mention that the arguments are evaluated differently. In the conditional version, if the result of the entire operation can be determined by the first argument, the second argument is not evaluated. This is called short-circuit evaluation. Bitwise operations have to evaluate both sides in order to compute the final value.

例如:

x.foo() && y.bar()

这只会叫 y.bar()如果 x.foo()计算结果为真正。相反,

This will only call y.bar() if x.foo() evaluates to true. Conversely,

x.foo() || y.bar()

将只调用 y.bar()如果 x.foo()计算结果为

这篇关于是什么逻辑和条件AND,OR在C#中的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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