Conditional Ternary Operator “?” C#, Why (not always) is not “If sentence” replacement
Tags: csharp, dotnet, programming, dotnetcore
Published: 2022-02-10 19:32:26

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:

{% gist https://gist.github.com/gabrielcastillocl/cb48b8ef32aae100c1ff37d3061586c5 %}

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.
{% gist 
https://gist.github.com/gabrielcastillocl/cb48b8ef32aae100c1ff37d3061586c5
%}

- 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) ) ==> ( a<b? a : (a>b? a : b) ), looks this example.
{% gist
https://gist.github.com/gabrielcastillocl/6583cdfda0a4ae8c29256e00a2c9e020 %}

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.

{% gist
https://gist.github.com/gabrielcastillocl/9336f4959d4b114e1dff1df6a4dc9935 %}

## 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:

{% gist
https://gist.github.com/gabrielcastillocl/bf7e94af54ac575c58b6bf6beec7894e %}

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
{% gist
https://gist.github.com/gabrielcastillocl/40a8670458454a8053c560f5689e1412 %}

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.