Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Operators and Expressions

C# logical operators

Reference for C# logical operators: short-circuit && and ||, negation !, non-short-circuit & and |, exclusive or ^, nullable bool logic and pattern keywords.

C# has two families of logical operators. The conditional operators && (and) and || (or) short-circuit: they stop evaluating as soon as the result is known. The boolean operators &, | and ^ always evaluate both operands. The unary ! negates a bool.

For conditions, use && and ||. Reach for & and | only when you want both sides to run (for example, both methods have side effects) or when you work with bool? and flags. ^ is exclusive or: true when exactly one side is true.

C#
if (user != null && user.IsActive) { /* user is never null here */ }
bool toggled = !isOn;

Try it

C# Short-circuit versus full evaluation

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Nullable bool, precedence and De Morgan

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
bool r1 = a && b;   // conditional and (short-circuit)
bool r2 = a || b;   // conditional or  (short-circuit)
bool r3 = !a;       // negation
bool r4 = a & b;    // boolean and (both sides evaluated)
bool r5 = a | b;    // boolean or  (both sides evaluated)
bool r6 = a ^ b;    // exclusive or
Operator Result
a && b false if a is false (b not evaluated); otherwise b.
a || b true if a is true (b not evaluated); otherwise b.
!a The opposite of a.
a & b true only if both are true. Both operands are always evaluated.
a | b true if either is true. Both operands are always evaluated.
a ^ b true if exactly one operand is true.

Returns: bool (or bool? for the nullable forms of !, &, |, ^).

Errors: && and || do not accept integers (1 && 0 is a compile error), and they are not defined on bool?; use & and | there. A user-defined && needs both an overloaded & and the true/false operators.

Usage notes

  • Order matters for safety. Put the cheap or protective test first: list != null && list.Count > 0. Because the right side is skipped, it can depend on the left side being true.
  • Precedence from highest to lowest: !, then &, ^, |, then &&, then ||. Add parentheses when mixing && and ||; the compiler does not warn about the common mistake.
  • Side effects in conditions are easy to miss with short-circuiting. If the right side must always run, evaluate it into a variable first or use & / |.
  • Three-valued logic with bool?. false & null is false and true | null is true, since the missing value cannot change the outcome. Everything else involving null gives null. An if cannot take a bool? directly; compare with == true or use ??.
  • ^ also works as "not equal" for booleans: a ^ b equals a != b.
  • Integers and enums. The same &, | and ^ symbols are bitwise there. See Bitwise and shift operators.
  • Patterns. and, or and not are pattern combinators, not operators: use them inside is and switch patterns (x is not null, c is >= 'a' and <= 'z').
  • Compound forms &=, |= and ^= exist; there is no &&= or ||=.
  • Performance: short-circuit operators compile to conditional jumps, which the CPU may mispredict. Branch-free & and | on simple bools can occasionally be faster in tight loops, but measure before using them.