Language: EN

csharp-condicional-ternario

What is and how to use the ternary operator

The ternary operator, also known as the conditional operator, is a useful tool in C# that allows for concise conditional evaluations 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.

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 to be assigned if the condition is true.
  • False Expression: The value to be 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

Let’s say 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 more compactly.

int number = 10;
string result = (number % 2 == 0) ? "even" : "odd";

As can be seen, the ternary conditional allows us to directly assign the evaluation result to the “result” variable in a more concise and readable 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, “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 operation are very clear. If you see that it harms readability, use another option.

Practical Examples

Compare two numbers and determine the greater one

Suppose we have two variables, “a” and “b”, representing two integers, and we want to determine which of the two 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 category variable 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/ma'am" : "Hello";
}

This GetGreeting method returns “Good morning, sir/ma’am” if the isFormal parameter is true; otherwise, it returns “Hello”.