?: Operator (C# Reference)

The conditional operator ?:, commonly known as the ternary conditional operator, evaluates a Boolean expression, and returns the result of evaluating one of two expressions, depending on whether the Boolean expression evaluates to true or false. Beginning with C# 7.2, the conditional ref expression returns the reference to the result of one of the two expressions.

The syntax for the conditional operator is as follows:

condition ? consequence : alternative

The condition expression must evaluate to true or false. If condition evaluates to true, the consequence expression is evaluated, and its result becomes the result of the operation. If condition evaluates to false, the alternative expression is evaluated, and its result becomes the result of the operation. Only consequence or alternative is evaluated.

The type of consequence and alternative must be the same, or there must be an implicit conversion from one type to the other.

The conditional operator is right-associative, that is, an expression of the form

a ? b : c ? d : e

is evaluated as

a ? b : (c ? d : e)

The following example demonstrates the usage of the conditional operator:

double sinc(double x) => x != 0.0 ? Math.Sin(x) / x : 1;

Console.WriteLine(sinc(0.1));
Console.WriteLine(sinc(0.0));
// Output:
// 0.998334166468282
// 1

Conditional ref expression

Beginning with C# 7.2, you can use the conditional ref expression to return the reference to the result of one of the two expressions. You can assign that reference to a ref local or ref readonly local variable, or use it as a reference return value or as a ref method parameter.

The syntax for the conditional ref expression is as follows:

condition ? ref consequence : ref alternative

Like the original conditional operator, the conditional ref expression evaluates only one of the two expressions: either consequence or alternative.

In the case of the conditional ref expression, the type of consequence and alternative must be the same.

The following example demonstrates the usage of the conditional ref expression:

var smallArray = new int[] { 1, 2, 3, 4, 5 };
var largeArray = new int[] { 10, 20, 30, 40, 50 };

int index = 7;
ref int refValue = ref ((index < 5) ? ref smallArray[index] : ref largeArray[index - 5]);
refValue = 0;

index = 2;
((index < 5) ? ref smallArray[index] : ref largeArray[index - 5]) = 100;

Console.WriteLine(string.Join(" ", smallArray));
Console.WriteLine(string.Join(" ", largeArray));
// Output:
// 1 2 100 4 5
// 10 20 0 40 50

For more information, see the feature proposal note.

Conditional operator and an if..else statement

Use of the conditional operator over an if-else statement might result in more concise code in cases when you need conditionally to compute a value. The following example demonstrates two ways to classify an integer as negative or nonnegative:

int input = new Random().Next(-5, 5);

string classify;
if (input >= 0)
{
    classify = "nonnegative";
}
else
{
    classify = "negative";
}

classify = (input >= 0) ? "nonnegative" : "negative";

Operator overloadability

The conditional operator cannot be overloaded.

C# language specification

For more information, see the Conditional operator section of the C# language specification.

See also