?: Operator (C# Reference)

The conditional operator (?:), commonly known as the ternary conditional operator, returns one of two values depending on the value of a Boolean expression. Following is the syntax for the conditional operator.

condition ? first_expression : second_expression;  

Beginning with C# 7.2, the first_expression and second_expression my be ref expressions:

ref condition ? ref first_expression : ref second_expression;  

The result may be assigned to a ref or ref readonly variable, or to a variable with neither modifier.

Remarks

The condition must evaluate to true or false. If condition is true, first_expression is evaluated and becomes the result. If condition is false, second_expression is evaluated and becomes the result. Only one of the two expressions is evaluated. This is particularly important for expressions where the result is a ref, as the following is valid:

ref (storage != null) ? ref storage[3] : ref defaultValue;

The reference to storage is not evaluated when storage is null.

When the result is a value, the type of first_expression and second_expression must be the same, or there must be an implicit conversion from one type to the other. When the result is a ref, the type of first_expression and second_expression must be the same.

You can express calculations that might otherwise require an if-else construction more concisely by using the conditional operator. For example, the following code uses first an if statement and then a conditional operator to classify an integer as positive or negative.

int input = Convert.ToInt32(Console.ReadLine());  
string classify;  
  
// if-else construction.  
if (input > 0)  
    classify = "positive";  
else  
    classify = "negative";  
  
// ?: conditional operator.  
classify = (input > 0) ? "positive" : "negative";  

The conditional operator is right-associative. The expression a ? b : c ? d : e is evaluated as a ? b : (c ? d : e), not as (a ? b : c) ? d : e.

The conditional operator cannot be overloaded.

Example

The following example shows the conditional operator whose result is a value:

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

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

The following alternative shows the conditional operator where the result is a reference:

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

int index= new Random().Next(0, 9);
ref int refValue = ref ((index < 5) ? ref smallArray[index] : ref largeArray[index - 5]);
Console.WriteLine(refValue);

See Also