I am a bit confused by the MSDN C# documentation which states that & and | are logical operators and that && and || are conditional operators.

I keep calling &&, || and ! logical operators, so I am wrong?

有帮助吗?

解决方案

I am a bit confused by the MSDN C# documentation which states that & and | are logical operators and that && and || are conditional operators. I keep calling &&, || and ! logical operators, so I am wrong?

No; you're correct.

There are numerous small, mostly unimportant nomenclature errors in the MSDN documentation; I tried to get as many of them out as I could, but in cases where it is not egregiously wrong and misleading, it's not always a wise use of time. Go to the specification if you want a definitive statement about the name of a C# feature.

So: the relevant authority is the C# specification, which states in section 7.11:

The &, ^, and | operators are called the logical operators.

It then goes on to further break down the built-in logical operators into integer, enumeration, Boolean and nullable-Boolean logical operators. There are also user-defined logical operators; see the spec for details.

In section 7.12 we have

The && and || operators are called the conditional logical operators. They are also called the “short-circuiting” logical operators.

So all of them are logical operators. Some of them are conditional logical operators.

What makes the conditional logical operators conditional? One might make a specious guess that it is because they are typically used in conditional statements (if) or conditional expressions (? :). The real reason is given by the specification:

The && and || operators are conditional versions of the & and | operators: The operation x && y corresponds to the operation x & y, except that y is evaluated only if x is not false. The operation x || y corresponds to the operation x | y, except that y is evaluated only if x is not true.

The conditional logical operators are thus named because the right hand operand is evaluated conditionally depending on the value of the left hand operand.

We can see this more vividly by noting that the conditional logical operators are just "syntactic sugars" for conditional expressions. x && y is simply a more pleasant way to write x ? y : false, and x || y is simply a more pleasant way to write x ? true : y. The conditional logical expressions are actually conditional expressions.

There is also a user-defined form of the conditional logical operator, and it is a little tricky. See the specification for details.

Further reading, if this subject interests you:

其他提示

In C# these are all logical operators.

int x = 0xABCD & 0xFF // x == 0xCD

&& and || are called "conditional logical operators" because they are short-circuiting.

bool someOtherCondition = true;
if (x == 0xEF && someOtherCondition) // someOtherCondition is not evaluated, 
                                     // because x == 0xEF is false

Note that this terminology differs from language to language. In C and C++ && and || are just Logical Operators. In Java, & and | are called Bitwise Operators, while C and C++ classifies them as Arithmetic Operators.

The point is that & and | are bitwise operators, meaning they are applied to and yield bit string values. And bitwise is a very used term among programmers.

For instance 0xff & 0x00 == 0x00, while 0xff | 0x00 == 0xff.

And && and || are applied to conditions, and yield the usual values of conditions; i.e. true and false.

For instance true && false == false, while true || false == true.

Therefore && and || could be called conditional operators, even though that is not an usual term among programmers.

Of course, every C, C++, Java and C# programmer knows all that. But I guess that the misunderstood happens because "conditional operator" is not a term frequently used by us programmers.

许可以下: CC-BY-SA归因
scroll top