Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The && operator in C# is the conditional logical AND operator. It evaluates two expressions and returns true if both operands evaluate to true; otherwise, it returns false. Its defining technical characteristic is short-circuit evaluation.
bool leftOperand = true;
bool rightOperand = false;
bool result = leftOperand && rightOperand; // Evaluates to false

Evaluation Mechanics and Short-Circuiting

The && operator enforces strict left-to-right evaluation with short-circuiting behavior. The execution flow is as follows:
  1. The left operand is evaluated.
  2. If the left operand evaluates to false, the overall expression cannot possibly be true. The operator immediately returns false, and the right operand is not evaluated.
  3. If the left operand evaluates to true, the right operand is evaluated, and its result becomes the result of the entire expression.
This behavior contrasts with the boolean logical AND operator (&), which always evaluates both operands regardless of the left operand’s result when applied to bool types.
bool EvaluateLeft() 
{
    System.Console.WriteLine("Left evaluated.");
    return false;
}

bool EvaluateRight() 
{
    System.Console.WriteLine("Right evaluated.");
    return true;
}

// The following line will only print "Left evaluated."
// EvaluateRight() is bypassed due to short-circuiting.
bool result = EvaluateLeft() && EvaluateRight(); 

Type Constraints and Overloading

  • Standard Types: For standard boolean logic, both operands must be of type bool.
  • Nullable Booleans: The && operator does not directly support bool? (nullable boolean) operands. To evaluate nullable booleans, you must explicitly cast them, access the .Value property, or use the boolean logical AND operator (&), which supports three-valued logic.
  • User-Defined Types: The && operator itself cannot be explicitly overloaded. However, user-defined types can support && evaluation by overloading the true, false, and & operators. When evaluated this way:
    • The custom type does not need to be implicitly convertible to bool.
    • The short-circuiting logic relies on the overloaded false operator applied to the left operand.
    • The overall && expression evaluates to the return type of the overloaded & operator (typically the custom type itself), rather than strictly returning a bool.

Precedence and Associativity

  • Associativity: The && operator is left-associative. An expression like a && b && c is parsed and evaluated as (a && b) && c.
  • Precedence: It holds a lower precedence than equality operators (==, !=) and the boolean/bitwise logical operators (&, ^, |), but a higher precedence than the conditional logical OR operator (||).
int a = 1;
int b = 1;
bool c = false;
bool d = true;

// Due to precedence, this is evaluated as:
// bool precedenceResult = ((a == b) && c) || d;
bool precedenceResult = a == b && c || d; // Evaluates to true
Master C# with Deep Grasping Methodology!Learn More