The ternary operator (also known as the conditional operator) is a tool in C# that allows us to perform conditional evaluations concisely in a single line of code.
It is a shorthand way of expressing an if-else
statement in situations where we want to assign a value based on a specific condition.
If you want to learn more about the Ternary Operator
check out the Introduction to Programming Course read more
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 variable “result” more concisely.
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, “positive” is assigned if the number is greater than 0, “negative” if it is less than 0, and “zero” if it is equal to 0.
Very important, use nesting only when the purpose and functionality are clear. If it detracts from readability, consider using another option.
Practical Examples
Compare two numbers and determine the greater one
Suppose we have two variables, “a” and “b”, representing two integer numbers, and we want to determine which one is greater. 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 18 or older; 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 method GetGreeting
returns “Good morning, sir/madam” if the isFormal
parameter is true; otherwise, it returns “Hello”.