C# Basics

C# Conditional Operator

C# provides a conditional operator, which is sometimes called C# ternary or question operator. The C# conditional operator “?:” uses the Boolean value of an expression to determine which two other expressions must be calculated and returned as a result. Actually, this operator is a shorthand method of writing a simple single line if else statement. The C# conditional operator makes your code shorter and easier to read.

It uses three operands on one line. The first operand is a Boolean expression that evaluates to true or false. If the expression is true, the value of the second operand is returned; otherwise, the value of the third is returned.


Syntax

The syntax works like this:

(Condition) ? expression1 : expression2;

Here, the Condition is evaluated to obtain a Boolean value, and the result of the operator is either expression1 or expression2 based on this value. In other words, if Condition is true, expression1 is evaluated and becomes the result of the operation. Otherwise, expression2 is evaluated and becomes the result of the operation. A conditional expression never evaluates both expression1 and expression2.

C# Ternary operator C# Ternary operator flow diagram
C# ternary operator
C# ternary operator flow diagram

The ternary conditional operator is right-associative; meaning, it is evaluated from right to left.


Restrictions

  • If you want to return a value, then use a conditional statement. Otherwise, use an if-else statement.
  • The operand1 and operand2 type must be the same, or an implicit conversion must exist from one type to the other.
  • C# conditional operator can only be used in assignment statements.

Example 1

bool loggedIn = true;
string status = loggedIn ? "Online" : "Offline";
Console.WriteLine($"User is {status}"); // Print

In the above code, the result of the ternary operator is one of two strings, both of which may be assigned to status. The choice of which string to assign depends on the value of the loggedIn variable being true or false. In this case, a value of true results in the first string being assigned, and a value of false results in the second string being assigned.

The above code is equivalent to the following:

bool loggedIn = true;
string status = String.Empty;
if (loggedIn)
  status = "Online";
else
  status = "Offline";

Console.WriteLine($"User is {status}"); // Print

Run Demo

OUTPUT

User is Online


Example 2

int number = 3;
string value = (number % 2 == 0) ? "Even" : "Odd";
Console.WriteLine($"{number} is {value}");

If conditional expression (number % 2 == 0) in the above example evaluates to true, the string “Even” is returned; Otherwise, string “Odd”. Thus, this statement with the conditional operator performs essentially the same task as the first if-else statement.

The above code is equivalent to the following:

int number = 3;
string value = String.Empty;
if (number % 2 == 0)
  value = "Even";
else
  value = "Odd";

Console.WriteLine($"{number} is {value}");

Run Demo

OUTPUT

3 is Odd


Example 3

int a = 6; 
int b = 4; 

Console.WriteLine(a > b ? "a is greater than b" : "a is less than b");
Console.WriteLine(a < b ? "a is greater than b" : "a is less than b");

b = 6; 
Console.WriteLine(a == b ? "a is equal to b" : "a is not equal to b");
Console.WriteLine(a != b ? "a is equal to b" : "a is not equal to b");
Console.WriteLine((6+2 == 8) ? 8 : 6);

Run Demo

OUTPUT

a is greater than b
a is less than b
a is equal to b
a is not equal to b
8


Operators

Operators in C# can be separated into different categories. Some of them are listed below:


C# Reference | Microsoft Docs