Conditional operator (?:) in C# is used as a single line if-else assignment statement, it is also know as Ternary Operator in C# . It evaluates a boolean expression and on the basis of the evaluated True and False value executes corresponding statement.

Precisely, In C# Conditional operator (?:) can be explained as follows.

It has three operands : condition, consequence and alternative. The conditional expression returns a Boolean value as true or false . If the value is true, then it evaluates the consequence expression. If false, then it evaluates the alternative expression.

Syntax of conditional operator (?:) in C#

It uses a question mark ( ? ) operator and a colon ( : )  operator , the general format is as follows.

Condition (Boolean expression) ? consequence (if true) : alternative (if false)

Lets look at below example, here we are doing a conditional check and based on the true/false, assigning the variable value to taxPercentage .

In above case, If isSiniorCitizen == true then taxPercentage will be 20 else it will be 30, in the above case it sets to 20 since we have already set the boolean value isSiniorCitizen = true .

Example of (?:) operator in C#

Lets look at an example.

In above example code snippet, the boolean expression String.IsNullOrEmpty(LastName) returns True value, so the consequence statement executes, which gives output as “Ramesh”

Conditional operator figure 1.0
Conditional Operator 1.0

Lets take another example, where conditional expression returns False value.

In above example, the boolean expression String.IsNullOrEmpty(LastName returns False value, so the alternative statement (FirstName + " " + LastName) executes, which gives output as “Ramesh Kumar”

Conditional operator figure 1.1
Conditional Operator 1.1

Conditional operator(?:) vs Null Coalescing (??) Operator ?

In the case of Null coalescing operator (??), It checks for the null-ability of first operand and based on that assigns the value. If the first operand value is null then assigns second operand else assigns the first.

Lets have a look at below expression.

In the above example, If y is null then z will be assigned to x else y . For more details on Null coalescing operator (??), please check here

We can rewrite above expression using conditional operator (?:) as follows.

Here, if y is null (that means if the expression (Y == null) return true) then assign z else assign y to x .

In single line if else statement :

Lets take below if-else statement.

In above if-else statement, boolean expression determines variable assignment. This is the right scenario where we can use Conditional operator (?:) as a replacement of if-else statement.

In above example, here in this case i >= 5  is a True condition, hence 10 * 5  will be executed and 50 will be assigned to x. Lets take below example.

In above example, here condition i >= 5 is  False, hence 10 * 4  will be executed and 40 will be assigned to x.

Nested conditional operator (?:) in C#

In some scenarios, where there are cascading if-else conditions of variable assignment. We can use chaining conditional operators to replace cascading if-else conditions to a single line by including a conditional expression as a second statement.

Lets take below example of cascading/nested if-else statement.

Above cascading if-else condition can be re-written as follows.

Thanks for your time 🙂 feel free to provide your feedback and do check my other blogs on Null Coalescing Operator (??) in C#  and  Null-Conditional Operator in C# (?.)