Using the null-coalescing operator is a concise way to check if a variable or function has a value before returning it and if the variable or function does not have a value then return a default value. In C#, two consecutive question mark characters ?? is used as the null-coalescing operator.
Let’s use an example application where a user is ordering a physical product. The product ordering user interface has an option to select a color but it is not required. If the user does not select a color then the user selected color variable in our application will be null. If no color is selected by the user then our company has decided to use the blue product.
Comparing If Else Conditions, Ternary, And Null-Coalescing
Using the normal if else conditional statements, the below code could be used to check if the selectedColor variable has a color that the user selected then that color value is assigned to the productColor variable. If the user did not select a color value then the selectedColor variable is null and the default string value of “blue” is assigned to the productColor variable.
if (selectedColor != null) { productColor = selectedColor; } else { productColor = "blue"; }
The same code can also be written in a shorter version using ternary like the following.
productColor = (selectedColor != null) ? selectedColor : "blue";
Here is the much cleaner null-coalescing version.
productColor = selectedColor ?? "blue";
In this example, the selectedColor variable and the string “blue” are the operands of the statement. The null-coalescing operator will start from the left and return the first operand that has a non null value. The value of the selectedColor variable will be checked. If selectedColor is not null then its value will be returned and assigned to the productColor variable. If selectedColor is null then “blue” will be returned and assigned to the productColor variable.
Chaining Multiple Operands
Null-coalescing can be done with multiple operands. Let’s update our product ordering example so that when assigning a product color our company wants to use the following steps. When a step provides a color the next steps do not need to be used.
1. Check if the user selected a color.
2. Check if there is overstock of a certain color product.
3. Check if there is a certain promotional color product to be used for the month.
4. Use blue as the default product color.
This example would require the following more lengthy if else conditions.
if (selectedColor != null) { productColor = selectedColor; } else if (overstockColor != null) { productColor = overstockColor; } else if (monthlyColor != null) { productColor = monthlyColor; } else { productColor = "blue"; }
We can quickly see that the below null-coalescing version is much quicker to type and easier to read.
productColor = selectedColor ?? overstockColor ?? monthlyColor ?? "blue";
This type of example is useful to show why it is called null-coalescing. One of the definitions for coalesce from the Merriam-Webster Dictionary is “to arise from the combination of distinct elements”. Multiple elements are joined together and one non null value is the result that is returned.
The null-coalescing operator again starts from the left and checks each operand and returns the first operand that is not null. If the user had not selected a value causing the selectedColor variable to be null and the overstockColor variable had a value of “purple” then “purple” would be assigned to the productColor variable. If all three possible variables were null then the string “blue” would be returned and assigned to the productColor variable.
Let’s review a few scenarios by displaying a variable’s value above it in the below screenshots.
Example 1

In the above example, the selectedColor variable is the left most operand so it is checked first. It has a value of “red” so the string “red” is immediately returned and assigned to the productColor variable. The remaining three operands are not checked.
Example 2

In the above example, the selectedColor variable is the left most operand so it is checked first. It is null so the operand to its right is checked next. The overstockColor variable has a value of “purple” so the string “purple” is returned and assigned to the productColor variable.
Example 3

In the above example, the selectedColor variable is checked first and it is null. The overstockColor variable is checked next and it is null. The monthlyColor variable is checked next and it is null. The next operand checked is the string “blue” that is used as the default backup value when the other variables have no value assigned to them. The string “blue” is returned and assigned to the productColor variable.
Conclusion
From this article you can see that null-coalescing is used when you need make sure a value is provided. The benefits of using null-coalescing is that it can be much quicker to type and read.
References:
https://www.youtube.com/watch?v=YJGGmTNHPeo
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-coalescing-operator
https://www.pluralsight.com/guides/using-conditional-and-null-coalescing-operators