Skip to main content

How to Combine Conditions with the && and || Operators in C#

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
<left> && <right>
<left> || <right>
PartWhat 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 .cs file with dotnet run. This guide uses .NET SDK 10.0.404.

Step 1: Write the Code​

Create a file named main.cs in an empty folder:

main.cs, which prints IsPositive of -1 and IsEven of 4 combined with and, IsPositive of 2 and IsEven of 4 combined with and, IsPositive of 2 or IsEven of 3, and name is not null and name.Length greater than 2 for a null name, where IsPositive and IsEven print their own call before returning.

main.cs
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​

Terminal
dotnet run main.cs
Output
IsPositive(-1)
False
IsPositive(2)
IsEven(4)
True
IsPositive(2)
True
False

A zsh terminal where dotnet run main.cs prints the calls IsPositive and IsEven made and the results False, True, True and False.

Read the indented lines as "this method ran":

  1. IsPositive(-1) && IsEven(4): -1 isn't positive, so the answer was already False and IsEven never ran.
  2. IsPositive(2) && IsEven(4): both had to be checked, and both were true.
  3. IsPositive(2) || IsEven(3): the left side was true, which is enough for ||, so IsEven(3) never ran.
  4. name != null && name.Length > 2: name was null, so name.Length was 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:

main.cs
Console.WriteLine(IsPositive(-1) & IsEven(4));
Output
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​

MistakeWhat happensDo this instead
Writing & or | in a conditionBoth sides always run, so a null check doesn't protect the second sideUse && and ||
Putting the null check second, as in name.Length > 2 && name != nullname.Length runs first and throws for nullPut the check that guards the other first
Mixing && and || without parentheses&& binds tighter than ||, which may not be what you meantAdd parentheses to show the grouping

Troubleshooting​

SymptomLikely causeFix
NullReferenceException in a combined conditionThe null check comes after the member access, or & was usedCheck for null first, with &&
A method in a condition sometimes doesn't run&& or || skipped it because the left side decidedCall 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​