Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Operators and Expressions

C# conditional operator ?:

Reference for the C# conditional (ternary) operator ?: with typing rules, target-typed results, nesting, ref conditional and when to prefer if or switch.

The conditional operator condition ? whenTrue : whenFalse evaluates condition, then evaluates and returns exactly one of the two branches. It is C#'s only ternary operator, and it is an expression: it can appear wherever a value is expected, for example in an initializer, argument or return.

Use it for short value choices. If the branches need statements, or the nesting gets deep, an if statement or a switch expression reads better.

C#
string label = count == 1 ? "item" : "items";

Try it

C# Choosing a value

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Types of the two branches

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
result = condition ? consequent : alternative;
ref var r = ref (condition ? ref a : ref b);
Part Description
condition A bool expression.
consequent Evaluated and returned when the condition is true.
alternative Evaluated and returned when the condition is false.

Returns: the value of the chosen branch. The type is the common type of the branches (one must convert implicitly to the other), or, since C# 9, the type of the target the result is assigned to.

Errors: if the branches have no common type and no target type exists, the code does not compile. var x = cond ? 1 : "a"; is such a case.

Usage notes

  • Right-associative. a ? b : c ? d : e means a ? b : (c ? d : e). Format long chains in one column, as in the grade example, or switch to a switch expression.
  • Only one branch runs, so the other can safely contain a call that would fail or be expensive when the condition is false.
  • Precedence is low. Wrap it in parentheses inside string interpolation (the : would otherwise start a format string), and when combining with ?? or arithmetic.
  • Both branches should produce a value. A branch may be a throw expression; void calls are not allowed. Use if for statements.
  • Mixed numeric branches. cond ? 1 : 2.0 is a double.
  • Nullable results. cond ? 1 : null works only with a nullable target such as int?; a cast ((int?)1) or default helps older code.
  • Prefer alternatives when appropriate: ?? for null defaults, Math.Max, a lookup dictionary, or a switch expression for more than two outcomes.
  • Readability. Avoid nesting beyond two levels and avoid side effects in the condition.
  • Performance: compiles to a conditional jump, or to a conditional move for simple values; no overhead compared with if/else.