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 OR operator. It performs a logical OR operation using short-circuit evaluation. The right-hand operand is evaluated only if the left-hand operand does not definitively determine the outcome of the expression.
var result = leftOperand || rightOperand;

Evaluation Mechanics

For built-in boolean types, the operator processes operands from left to right and returns a bool:
  1. If the left-hand operand evaluates to true, the expression yields true. The right-hand operand is bypassed entirely (short-circuited), and any side effects within it do not occur.
  2. If the left-hand operand evaluates to false, the right-hand operand is evaluated, and its boolean value becomes the result of the entire expression.

Truth Table and Execution Flow

The following table demonstrates the outcome and short-circuiting behavior for bool types:
leftOperandrightOperandresultWas rightOperand evaluated?
trueany valuetrueNo
falsetruetrueYes
falsefalsefalseYes

Syntax Visualization of Short-Circuiting

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

// "Right operand evaluated." is NOT printed to the console.
// The evaluation of the expression short-circuits after reading the 'true' literal.
bool shortCircuited = true || EvaluateRight();

// "Right operand evaluated." IS printed to the console.
// The left operand is false, forcing evaluation of the right operand.
bool fullyEvaluated = false || EvaluateRight();

Technical Characteristics

  • Precedence: The || operator has lower precedence than the conditional logical AND operator (&&) but higher precedence than the null-coalescing operator (??) and assignment operators (=).
  • Associativity: It is left-associative. An expression like a || b || c is evaluated as ((a || b) || c).
  • Type Constraints: For built-in types, the || operator is defined exclusively for bool. It does not support three-valued logic; attempting to use || with bool? (nullable boolean) operands results in compiler error CS0019. However, the operator can also be applied to dynamic operands (where binding is deferred to runtime) and qualifying user-defined types.
  • User-Defined Types: The || operator cannot be explicitly overloaded. However, user-defined types can implicitly support the || operator by overloading the standard logical OR operator (|) alongside operator true and operator false. In this scenario, evaluating x || y returns an instance of the user-defined type T, not a bool. The evaluation resolves as follows:
    • If T.true(x) is true, the expression evaluates to x (short-circuited).
    • If T.true(x) is false, the expression evaluates to x | y.
  • Contrast with |: Unlike the standard logical OR operator (|), which always evaluates both operands regardless of the left operand’s value, || guarantees execution optimization via short-circuiting.
Master C# with Deep Grasping Methodology!Learn More