How to Combine Conditions with the && and || Operators in C#
Real conditions come in combinations: the number must be positive and even, the user must be an admin or the owner. In C#, && means both sides must be true and || means one is enough. Both also skip the right side when the left side already decides the answer.
Syntax
<left> && <right>
<left> || <right>
| Part | What it does |
|---|---|
<left> && <right> | true only when both sides are true. When <left> is false, <right> isn't evaluated. |
<left> || <right> | true when at least one side is true. When <left> is true, <right> isn't evaluated. |
Microsoft's Boolean logical operators reference: "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." And for ||: "If x evaluates to true, y isn't evaluated."
The Example
Two small methods, IsPositive and IsEven, print their own name every time they're called, so the output shows which ones C# actually ran. The program combines them with && and ||, and then checks a name that is null.
Prerequisites
- The .NET 10 SDK or newer, which runs a single
.csfile withdotnet run. This guide uses .NET SDK 10.0.404.
Step 1: Write the Code
Create a file named main.cs in an empty folder:

Console.WriteLine(IsPositive(-1) && IsEven(4));
Console.WriteLine(IsPositive(2) && IsEven(4));
Console.WriteLine(IsPositive(2) || IsEven(3));
string? name = null;
Console.WriteLine(name != null && name.Length > 2);
static bool IsPositive(int n)
{
Console.WriteLine($" IsPositive({n})");
return n > 0;
}
static bool IsEven(int n)
{
Console.WriteLine($" IsEven({n})");
return n % 2 == 0;
}
Step 2: Run It
dotnet run main.cs
IsPositive(-1)
False
IsPositive(2)
IsEven(4)
True
IsPositive(2)
True
False

Read the indented lines as "this method ran":
IsPositive(-1) && IsEven(4): -1 isn't positive, so the answer was alreadyFalseandIsEvennever ran.IsPositive(2) && IsEven(4): both had to be checked, and both weretrue.IsPositive(2) || IsEven(3): the left side wastrue, which is enough for||, soIsEven(3)never ran.name != null && name.Length > 2:namewasnull, soname.Lengthwas never read and nothing threw.
The command is the same in zsh, bash and PowerShell.
What & Does Differently
A single & gives the same true or false, but always evaluates both sides:
Console.WriteLine(IsPositive(-1) & IsEven(4));
IsPositive(-1)
IsEven(4)
False
IsEven(4) ran although the answer was already settled. The reference: "The & operator always evaluates both operands." With name != null & name.Length > 2, that would read Length of null and throw.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Writing & or | in a condition | Both sides always run, so a null check doesn't protect the second side | Use && and || |
Putting the null check second, as in name.Length > 2 && name != null | name.Length runs first and throws for null | Put the check that guards the other first |
Mixing && and || without parentheses | && binds tighter than ||, which may not be what you meant | Add parentheses to show the grouping |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
NullReferenceException in a combined condition | The null check comes after the member access, or & was used | Check for null first, with && |
| A method in a condition sometimes doesn't run | && or || skipped it because the left side decided | Call it before the condition when it must always run |
FAQ
Which comes first, && or ||?
&&. The reference lists the operators "starting from the highest precedence to the lowest", and conditional AND comes before conditional OR. So a || b && c means a || (b && c). Parentheses make that obvious to readers.
Can I chain more than two conditions?
Yes. a && b && c is true only when all three are, and evaluation stops at the first false. a || b || c stops at the first true.
Conclusion
&& needs both sides to be true, and || needs one. Both evaluate from left to right and skip the right side once the answer is known, which is what makes name != null && name.Length > 2 safe. Use & and | only when both sides must always run.
Additional Resources
- Boolean logical operators, on Microsoft Learn.
