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.
if (user != null && user.IsActive) { /* user is never null here */ } bool toggled = !isOn;
Try it
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.
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
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 & nullisfalseandtrue | nullistrue, since the missing value cannot change the outcome. Everything else involvingnullgivesnull. Anifcannot take abool?directly; compare with== trueor use??. ^also works as "not equal" for booleans:a ^ bequalsa != b.- Integers and enums. The same
&,|and^symbols are bitwise there. See Bitwise and shift operators. - Patterns.
and,orandnotare pattern combinators, not operators: use them insideisandswitchpatterns (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.