C# - Ternary Operator ?:
C# includes a decision-making operator ?:
which is called the conditional operator or ternary operator. It is the short form of the if else conditions.
condition ? statement 1 : statement 2
The ternary operator starts with a boolean condition.
If this condition evaluates to true then it will execute the first statement after ?
, otherwise the second statement after :
will be executed.
The following example demonstrates the ternary operator.
int x = 20, y = 10;
var result = x > y ? "x is greater than y" : "x is less than y";
Console.WriteLine(result);
Above, a conditional expression x > y
returns true, so the first statement after ?
will be execute.
The following executes the second statement.
int x = 10, y = 100;
var result = x > y ? "x is greater than y" : "x is less than y";
Console.WriteLine(result);
Thus, a ternary operator is short form of if else
statement. The above example can be re-write using if else
condition, as shown below.
int x = 10, y = 100;
if (x > y)
Console.WriteLine("x is greater than y");
else
Console.WriteLine("x is less than y");
Nested Ternary Operator
Nested ternary operators are possible by including a conditional expression as a second statement.
int x = 10, y = 100;
string result = x > y ? "x is greater than y" :
x < y ? "x is less than y" :
x == y ? "x is equal to y" : "No result";
Console.WriteLine(result);
The ternary 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
.
var x = 2, y = 10;
var result = x * 3 > y ? x : y > z? y : z;
Console.WriteLine(result);