The Ternary Conditional Operator

The conditional operator (?:) take three operands.  The first is a boolean expression to evaluate. If this expression is true, the value of the conditional operator is equal to the second operand.  If it’s false, the value of the conditional operator is equal to the third operand.
So for the expression
1
a ? b : c
the value of this expression is b if a is true, or c if a is false.
Here’s an example of using the conditional operator (assume that isRaining is a boolean):
1
string describeWeather = isRaining ? "Wet" : "Nice";
A conditional operator can always be rewritten as an if statement.  The previous example is equivalent to:
1
2
3
4
5
string describeWeather;
if (isRaining)
    describeWeather = "Wet";
else
    describeWeather = "Nice";


Using the conditional operator can lead to more concise code.