C# Logical Operators

Logical operators are used to combine or reverse true/false values. When one comparison is not enough, logical operators let you ask questions like "is the user logged in AND an admin?" or "is it a weekend OR a holiday?" They work on bool values and produce a bool result.

The three logical operators

C# has three logical operators. The AND operator && is true only when both sides are true. The OR operator || is true when at least one side is true. The NOT operator ! flips a value, turning true into false and false into true.

Logical operators

OperatorNameDescriptionExample
&&ANDTrue if both sides are true(x > 0) && (x < 10)
||ORTrue if at least one side is true(x < 0) || (x > 10)
!NOTReverses the result!(x == 5)

Combining two conditions with AND

int age = 25;
bool hasTicket = true;
if (age >= 18 && hasTicket)
{
    Console.WriteLine("You may enter.");
}

In the example above, the message only appears when both conditions are true: the person is at least 18 and they have a ticket. If either one is false, the whole condition is false and the block is skipped.

OR and NOT in action

int day = 6; // 6 = Saturday, 7 = Sunday
bool isWeekend = (day == 6 || day == 7);
Console.WriteLine(isWeekend);   // True
Console.WriteLine(!isWeekend);  // False (NOT reverses it)
Note: C# uses short-circuit evaluation. With &&, if the left side is already false, the right side is never checked because the result cannot be true. With ||, if the left side is already true, the right side is skipped. This can save work and prevent errors.
  • Use && when every condition must be true.
  • Use || when any one condition being true is enough.
  • Use ! to reverse a bool, which is handy for checking the opposite case.