Tech Talk A software developer on all things tech, and then some


What are the Ternary and Null Coalescing Operators and How To Use Them

Ever hear of a Ternary Operator or the Null Coalescing Operator and wonder what on earth that is? Some colleges will briefly mention the terms, but wont dive into how and why to use either operators. At first they might seem odd to use, but after you start using both of these it will speed the development process along in terms of writing simple evaluations and null checks.

What is the Ternary Operator?

Ternary operators are basically IF statements that require both a then and an else. They use different syntax then a normal if statement, and allow you to set the output equal to a variable. Let's start off with the normal syntax for an IF statement, then compare :

if (EvalAsBool)
    TheAnswer = true;
    TheAnswer = false;

Pretty easy and what you went over 1000 times in college. Now let's go over what using ternary operator looks like :

TheAnswer = EvalAsBool ? true : false;

As you can tell right away, it is much quicker to type out, while not sacrificing much readability. Just like any other assignment statement, you can set the response of the evaluation of the variable (really any other evaluation/comparison) to the variable on the left. The syntax is as follows :

[Variable to set the result to] = [Evaluation statement] ? [What to return if true] : [What to return if false];

What is the Null Coalescing Operator?

Sometimes you will see something that looks similar to the ternary operator using two question marks instead of the one, but it is actually something different. When you use the double question marks, that is actually called the Null Coalescing Operator. What this does is check if something is null versus doing a comparison like the ternary operator. This is worth mentioning however so you can use instead of a simple ternary operator when checking for null. Again, let's step through the different ways of checking for null, and we will end with the Null Coalescing Operator highlighting its ease of use.

First, the long way

if (TestIfNull == null)
    TestIfNull = string.Empty;

Now the ternary operator way

TestIfNull = TestIfNull == null ? string.Empty : TestIfNull;

Finally using the Null Coalescing Operator simplifying things way down

TestIfNull = TestIfNull ?? string.Empty;

So, now that you have seen a few examples, here is the syntax :

[Variable to set the result to] = [Variable you are checking for null] ?? [What to set if null]

When to Use The Operators?

There are four things I think about when creating and using some selection statement logic :

Are there multiple possibilities of  what variable I am checking for, besides a null or empty check?

I would use neither of the operators, but rather a switch case to handle the multiple scenarios.

What I am checking for basically consists of a few comparisons?

Again, neither of the operators, but an IF-Else statement instead. Since the Ternary Operator is all on one line, it is best to use it for simple comparisons.

A pretty basic, single comparison?

Use the Ternary Operator. This is where it will excel because of the quick to type syntax.

Am I just checking for null?

Use the Null Coalescing Operator. This is perfect to use for the quick null check and what to do if null is found.

Comments (0) Trackbacks (0)

No comments yet.

Leave a Reply

No trackbacks yet.

%d bloggers like this: