The ternary operator (also known as the conditional operator) is a C# tool that allows us to perform conditional evaluations in a concise way in a single line of code.
It is a shorthand way of expressing an if-else statement in situations where you want to assign a value based on a specific condition.
If you want to learn more, check out the Introduction to Programming Course
The ternary operator takes three operands. Its basic syntax is as follows:
condition ? true_expression : false_expression
- Condition: A boolean expression that evaluates to true or false.
- True Expression: The value assigned if the condition is true.
- False Expression: The value assigned if the condition is false.
The ternary operator returns the value of the true expression if the condition is true; otherwise, it returns the value of the false expression.
Basic Example
Suppose we want to determine if a number is even or odd and store the result in a variable called “result”. We could write the code as follows:
int number = 10;
string result;
if (number % 2 == 0)
{
result = "even";
}
else
{
result = "odd";
}
However, using a ternary conditional, we can simplify this code into a more compact form.
int number = 10;
string result = (number % 2 == 0) ? "even" : "odd";
The ternary conditional allows us to directly assign the result of the evaluation to the “result” variable in a more concise way.
Nesting
The ternary operator can be nested to perform more complex evaluations. For example,
int number = 10;
string result = (number > 0) ? "positive" :
(number < 0) ? "negative" :
"zero";
Console.WriteLine($"The number is {result}");
In this example, it assigns “positive” if the number is greater than 0, “negative” if it is less than 0, and “zero” if it is equal to 0.
Very important: only use nesting when the purpose and functionality remain very clear. If you see that it harms readability, use another option.
Practical Examples
Compare Two Numbers and Determine the Larger One
Suppose we have two variables, “a” and “b”, representing two integers, and we want to determine which of the two is larger. We could write the code as follows:
int a = 5;
int b = 8;
int greater;
if (a > b)
{
greater = a;
}
else
{
greater = b;
}
With the ternary conditional, we can reduce this code to a single line:
int a = 5;
int b = 8;
int greater = (a > b) ? a : b;
Again, the ternary conditional allows us to perform the evaluation and assignment in a single line, simplifying our code and making it easier to read.
Value Assignment
The ternary operator is useful when you need to assign a value based on a simple condition in a single line of code.
int age = 20;
string category = (age >= 18) ? "adult" : "minor";
Console.WriteLine($"The person is {category}");
This example assigns the string “adult” to the variable category if the age is greater than or equal to 18; otherwise, it assigns the string “minor”.
Return Value in Methods
The ternary operator can also be used to determine the return value of a method based on a condition.
public string GetGreeting(bool isFormal)
{
return isFormal ? "Good morning, sir/madam" : "Hello";
}
This GetGreeting method returns “Good morning, sir/madam” if the isFormal parameter is true; otherwise, it returns “Hello”.
