C# includes a decision-making operator ?: called the conditional operator or ternary operator. It is the short form of the if-else conditions.
Syntax:
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); OUTPUT: x is greater than y
Above, a conditional expression x > y returns true, so the first statement after ? will be executed.
