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, formally known as the ternary conditional operator, evaluates a boolean expression and returns the result of one of two mutually exclusive expressions based on that evaluation. It is an expression-based construct, meaning it resolves to a value, unlike the statement-based if-else control flow.
bool condition = true;
int consequent = 1;
int alternative = 0;

int result = condition ? consequent : alternative;

Evaluation Mechanics

The operator requires three operands:
  1. condition: An expression that must be implicitly convertible to bool, or a type that explicitly overloads the true and false operators.
  2. consequent: The expression evaluated and returned if the condition evaluates to true.
  3. alternative: The expression evaluated and returned if the condition evaluates to false.
The operator enforces short-circuit evaluation. The compiler guarantees that only one of the two result expressions is evaluated. If condition is true, consequent is evaluated and alternative is ignored. If condition is false, alternative is evaluated and consequent is ignored.

Type Resolution

The compiler must determine a single return type for the entire ternary expression. The rules for type resolution depend on the C# version:
  • Standard Type Resolution: There must be an implicit conversion from the type of the consequent to the type of the alternative, or vice versa. If neither conversion exists, or if both exist (creating an ambiguity), the compiler throws an error.
  • Target-Typed Conditional Expressions (C# 9.0+): If the consequent and alternative do not share a common type, the expression is valid provided both types are implicitly convertible to the target type of the assignment or context.
using System.Collections.Generic;

bool condition = true;

// Standard resolution: 'int' implicitly converts to 'double'
double result = condition ? 10.5 : 5; 

// Target-typed resolution (C# 9.0+): Both convert to 'IEnumerable<int>'
IEnumerable<int> sequence = condition ? new List<int>() : new int[5];

Associativity

The ternary operator is right-associative. When multiple conditional operators are chained, they are grouped from right to left.
bool a = true;
bool c = false;
int b = 1, d = 2, e = 3;

// The expression:
int chainedResult = a ? b : c ? d : e;

// Is evaluated by the compiler as:
int groupedResult = a ? b : (c ? d : e);

Ref Conditional Operator

Starting with C# 7.2, the ternary operator can be used to return a variable reference (ref) rather than a value. Both the consequent and alternative must be ref expressions of the exact same type. When assigning the result to a ref local, the entire ternary expression must be enclosed in parentheses to parse correctly.
bool condition = true;
int variable1 = 10;
int variable2 = 20;

ref int reference = ref (condition ? ref variable1 : ref variable2);

Restrictions

  • The ?: operator cannot be overloaded via user-defined operator overloading.
  • Because it is an expression, it cannot be used as a standalone statement; its result must be assigned, passed as an argument, or returned.
Master C# with Deep Grasping Methodology!Learn More