
The following table presents that semantics: x If either x or y evaluates to true, x | y produces true (even if another operand evaluates to null). The | operator produces false only if both its operands evaluate to false. If either x or y evaluates to false, x & y produces false (even if another operand evaluates to null). The & operator produces true only if both its operands evaluate to true. Nullable Boolean logical operatorsįor bool? operands, the & (logical AND) and | (logical OR) operators support the three-valued logic as follows: The logical OR operator | also computes the logical OR of its operands, but always evaluates both operands. In the following example, the right-hand operand of the || operator is a method call, which isn't performed if the left-hand operand evaluates to true: bool SecondOperand() If x evaluates to true, y isn't evaluated. The result of x || y is true if either x or y evaluates to true. The conditional logical OR operator ||, also known as the "short-circuiting" logical OR operator, computes the logical OR of its operands. The logical AND operator & also computes the logical AND of its operands, but always evaluates both operands. In the following example, the right-hand operand of the & operator is a method call, which isn't performed if the left-hand operand evaluates to false: bool SecondOperand() If x evaluates to false, y isn't evaluated. The result of x & y is true if both x and y evaluate to true. The conditional logical AND operator &, also known as the "short-circuiting" logical AND operator, computes the logical AND of its operands. The conditional logical OR operator || also computes the logical OR of its operands, but doesn't evaluate the right-hand operand if the left-hand operand evaluates to true.įor operands of the integral numeric types, the | operator computes the bitwise logical OR of its operands. In the following example, the right-hand operand of the | operator is a method call, which is performed regardless of the value of the left-hand operand: bool SecondOperand() 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. The result of x | y is true if either x or y evaluates to true. The | operator computes the logical OR of its operands. Console.WriteLine(true ^ true) // output: FalseĬonsole.WriteLine(true ^ false) // output: TrueĬonsole.WriteLine(false ^ true) // output: TrueĬonsole.WriteLine(false ^ false) // output: Falseįor operands of the integral numeric types, the ^ operator computes the bitwise logical exclusive OR of its operands. That is, for the bool operands, the ^ operator computes the same result as the inequality operator !=. 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. The ^ operator computes the logical exclusive OR, also known as the logical XOR, of its operands. The unary & operator is the address-of operator. The conditional logical AND operator & also computes the logical AND of its operands, but doesn't evaluate the right-hand operand if the left-hand operand evaluates to false.įor operands of the integral numeric types, the & operator computes the bitwise logical AND of its operands. In the following example, the right-hand operand of the & operator is a method call, which is performed regardless of the value of the left-hand operand: bool SecondOperand()Ĭonsole.WriteLine("Second operand is evaluated.") The & operator evaluates both operands even if the left-hand operand evaluates to false, so that the operation result is false regardless of the value of the right-hand operand. The & operator computes the logical AND of its operands.

The unary postfix ! operator is the null-forgiving operator. That is, it produces true, if the operand evaluates to false, and false, if the operand evaluates to true: bool passed = false Ĭonsole.WriteLine(!passed) // output: TrueĬonsole.WriteLine(!true) // output: False The unary prefix ! operator computes logical negation of its operand. For more information, see Bitwise and shift operators. Those operators evaluate the right-hand operand only if it's necessary.įor operands of the integral numeric types, the &, |, and ^ operators perform bitwise logical operations. Binary & (conditional logical AND) and || (conditional logical OR) operators.Those operators always evaluate both operands. Binary & (logical AND), | (logical OR), and ^ (logical exclusive OR) operators.The operators include the unary logical negation ( !), binary logical AND ( &), OR ( |), and exclusive OR ( ^), and the binary conditional logical AND ( &) and OR ( ||). The logical Boolean operators perform logical operations with bool operands.
