The logical Boolean operators perform logical operations with bool operands. The operators include the unary logical negation (!), binary logical AND (&), OR (|), and exclusive OR (^), and the binary conditional logical AND (&&) and OR (||).
- Unary ! (logical negation) operator.
- Binary & (logical AND), | (logical OR), and ^ (logical exclusive OR) operators. Those operators always evaluate both operands.
- Binary && (conditional logical AND) and || (conditional logical OR) operators. Those operators evaluate the right-hand operand only if it's necessary.
For operands of the integral numeric types, the &, |, and ^ operators perform bitwise logical operations. For more information, see Bitwise and shift operators.
Conditional logical AND operator &&
The conditional logical AND operator &&, also known as the "short-circuiting" logical AND operator, computes the logical AND of its operands. The result of x && y is true if both x and y evaluate to true. Otherwise, the result is false. If x evaluates to false, y isn't evaluated.
Logical OR operator |
The | operator computes the logical OR of its operands. The result of x | y is true if either x or y evaluates to true. Otherwise, the result is false.
The | operator evaluates both operands even if the left-hand operand evaluates to true, so that the operation result is true regardless of the value of the right-hand operand.
Logical NOT operator (!)
The NOT operator is a unary operator, as it operates on a single operand. The NOT operator inverts the value of a Boolean value. If the original value is true then the returned value is false; if the original value is false, the return value is true. The NOT operation is often known as the binary complement.
Logical exclusive OR operator ^
The ^ operator computes the logical exclusive OR, also known as the logical XOR, of its operands. The result of x ^ y is true if x evaluates to true and y evaluates to false, or x evaluates to false and y evaluates to true. Otherwise, the result is false. That is, for the bool operands, the ^ operator computes the same result as the inequality operator !=.
Conditional logical OR operator ||
The conditional logical OR operator ||, also known as the "short-circuiting" logical OR operator, computes the logical OR of its operands. The result of x || y is true if either x or y evaluates to true. Otherwise, the result is false. If x evaluates to true, y isn't evaluated.
Nullable Boolean logical operators
For bool? operands, the & (logical AND) and | (logical OR) operators support the three-valued logic as follows:
The
&operator producestrueonly if both its operands evaluate totrue. If eitherxoryevaluates tofalse,x & yproducesfalse(even if another operand evaluates tonull). Otherwise, the result ofx & yisnull.The
|operator producesfalseonly if both its operands evaluate tofalse. If eitherxoryevaluates totrue,x | yproducestrue(even if another operand evaluates tonull). Otherwise, the result ofx | yisnull.




0 Comments