Single Line Conditional Operators in .Net

conditional operators

Do you know that you can write a nested conditional statement in .net with true and false sides in a single line of code?

Yes, this is possible. It can be done using the ternary/binary If operators in vb.net and their equivalent operators, the conditional operator  ? :  and the null coalescing operator ??

In additional to the above, and particularly in C#, there is a feature, you can add as many as you want from the null coalescing operators it is called the chained null coalescing operator ?? ?? …etc.  you can somehow achieve a similar effect in vb.net using nested binary If operator, but It might make the code harder to read and maintain.

These conditional operators use Short-circuit evaluation to conditionally return one of two values. Short-circuit evaluation means the second argument is executed or evaluated only if the first argument does not suffice to determine the value of the expression. Read more about Short-circuit evaluation on wikipedia.

In this article I will be explaining in details the difference between ternary/binary conditional operators while providing code samples in both vb.net and C#

Let’s get started!

In VB.Net

The below code snippet shows the usage of the ternary operator in vb.net, this operator takes 3 arguments, first one is the boolean check, the second is the true part of the statement, the third is the false part of the statement;

That’s it, on a single line of code we check if the object _A is nothing, if it is nothing then instantiate a new object from class A , otherwise just return the object _A in _Result.

 

As for the binary operator, however, it takes only 2 arguments, the first one is the object of which we would like to check if its nothing, and the 2nd is the intended value of the false part of the statement. If the object is nothing, we return a new Instance of A, otherwise we return the instance of _A (which is in this case is not nothing).

 

In C#

The ternary and binary operators that are in vb.net have their similar match in C# , even though they are written in a different syntax, they behave exactly the same as their matching counterparts of vb.net.

The below snippet illustrates the usage of the ternary operator in C# (or the well-known conditional operator ) ? :

 

 And below is a snippet for the C# binary operator, or the other melodic name of it (null coalescing operator) ??

 

There is another usage of the binary operator, specifically the null coalescing operator in C#, something which is called a chained binary operator. This is mainly used with the C# syntax of null coalescing operator. The below snippet explains its usage more.

[code language=”csharp”]

As you have noticed we are chaining the null coalescing operator ??  as many times as we need. In simple words, this can be explained as , check if a is not null, return a, otherwise check if a2 is not null, return a2 otherwise return new instance of A.

Even though there is no direct matching counterpart of chained binary operator in vb.net, you can still achieve a similar result using a nested binary operator (just another for a binary operator inside another inside another and so on..) but I don’t recommend using it there, since it will make the code so hard to read, so in this case just stick to the traditional if statements instead.

Conclusion

The aforementioned operators can be used as a single line shorthand and prettier replacement for the traditional tangled if else statements. If you haven’t been using them, then it is time to start using them and therefore cutting down your lines of codes and make your code more readable and having less bugs.

Happy coding!

Bonus

Enjoy this calm and beautiful violin masterpiece by Felix Mendelssohn – Spring Song. Let me know if you like it 🙂

Leave a Reply