DEV Community

Gabriel Castillo
Gabriel Castillo

Posted on

Conditional Ternary Operator “?” C#, Why (not always) is not “If sentence” replacement

Maybe you are familiar with the ternary operator “?”, and we can agree that, in a logicall way, can be similar to an “if statement”, but they are not the same, and not always can be interchangeable.
So, in this article, I will try to explain, in a simple way, the difference and similitude between them.
By other hand, if you don’t know anything about the ternary operator, this article can be very useful too. Let’s begin.


What is an Operator?

The first topic we need to clarify is “What is an Operator”.
“An Operator is a symbol or character(s) that indicate how to be acting the operands from a expression”. From the previous sentence we need to rescue the word “expression”, and an expression is like a formule that gives a value result.

For instance, “a + b” is an expression, “a” and “b” are the operands and “+” is the operator, and “a+b” is the expression (or formule) that give as a result “a plus b”, and this results is assign to a variable or is used to be evaluated.

Other example about expression is this “a>=b”. In this expression the operator character is “>=” and the result of this expression can be true or false, depending the values of “a” and “b”.

The important word here is related with a expression (keep this word in mind).

C# has several Operators, and of the diferent types. Exists aritmethics operators (+, -, * , /), comparsion operators (>, <, <=, >=), the iquality, and so on.

So, wich mean the conditional ternary operator “?”

Well, the operator “?” is a ternary conditional operator. Is “ternary” because in his sintax are envolved 3 expressions, and is “conditional” because dependen of the evaluation of the one expression (true or false), to take the decision of evaluate one of other expression.

Looks the definition:

(condition expression) ? (consequent expression) : (alternative expression)

Looks now in action with a simple example code:

In this example “second>0” is the “condition expression”, and its evaluation returns a boolean value, if the result of this evaluation is “true”, then the “consequent expression” is evaluated, in this case “first / second” (division). By other hand, if the result of the “condition expression” is “false”, the “alternative expression” is evaluated (0), and the result of this entire evaluation is, in this case, asign to the “result” variable.

Talk about some kinds of rules related with this operator.

  • Before to C# 9.0, the alternative and consequent expression must be the same tipe. One alternative to do this in previous C# version is ralize an explicit conversion of type.

  • The operator is asociative to the right. If you encadenate ternary operators, this will be grouped from right to left. I mean (condition? (consequent): (alternative) ) ==> ( ab? a : b) ), looks this example.

In summary you can view the operator “?” is used to evaluate a condition and get a value realted with the condition. Looks this example now.

And with is the difference with If..Else Statement?

The “If…else” is a selection statement, and is used normally to generate a logic fork in our code. The global sintax is like this:

The “if else” dont give you a result value, I mean, you can’t assign “his result” to a variable (principal difference with a expression).

So the principal objective of “if else” is take a decision to realize one logic or another in your code.

But, exists cases when you can replace an if sentence with a ternary operator in a natural way, and the more natural case is when you use the “if sentence” to assignate one value or another depending the result of the if condition.

Looks this example, in the first option we use an if statement

You can agree that the second approach, using the ternary operator, is very much simple way, and is easy to read and understand.


So, in conclusion the ternary operator is an expression, and has similarities with the if statement, but not always can be one replacement by the other. I think the decision to changed is when the result of that change can give you a code more clear and understandable to your team.

Top comments (0)