Operators and Expressions

Boolean Logical Operators

  • 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.

Logical negation operator !

bool passed = false;
Console.WriteLine(!passed);  // output: True
Console.WriteLine(!true);    // output: False

Logical AND operator &

bool SecondOperand()
{
    Console.WriteLine("Second operand is evaluated.");
    return true;
}

bool a = false & SecondOperand();
Console.WriteLine(a);
// Output:
// Second operand is evaluated.
// False

bool b = true & SecondOperand();
Console.WriteLine(b);
// Output:
// Second operand is evaluated.
// True

Logical OR operator |

x
y
x&y
x|y

true

true

true

true

true

false

false

true

true

null

null

true

false

true

false

true

false

false

false

false

false

null

false

null

null

true

null

true

null

false

false

null

null

null

null

null

Logical exclusive OR operator ^

Conditional logical AND operator &&

Conditional logical OR operator ||

Operator Precedence

! > & > ^ > | > && > ||

null-coalescing operators (?? and ??=)

Last updated

Was this helpful?