TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
|| 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.
Evaluation Mechanics
For built-in boolean types, the operator processes operands from left to right and returns abool:
- If the left-hand operand evaluates to
true, the expression yieldstrue. The right-hand operand is bypassed entirely (short-circuited), and any side effects within it do not occur. - 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 forbool types:
leftOperand | rightOperand | result | Was rightOperand evaluated? |
|---|---|---|---|
true | any value | true | No |
false | true | true | Yes |
false | false | false | Yes |
Syntax Visualization of Short-Circuiting
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 || cis evaluated as((a || b) || c). - Type Constraints: For built-in types, the
||operator is defined exclusively forbool. It does not support three-valued logic; attempting to use||withbool?(nullable boolean) operands results in compiler error CS0019. However, the operator can also be applied todynamicoperands (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 (|) alongsideoperator trueandoperator false. In this scenario, evaluatingx || yreturns an instance of the user-defined typeT, not abool. The evaluation resolves as follows:- If
T.true(x)is true, the expression evaluates tox(short-circuited). - If
T.true(x)is false, the expression evaluates tox | y.
- If
- 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





