The hidden C# Operator - ??

How many times have you actually written your program to check if an object is null or not. Well! It isn’t difficult at all. Let me introduce you to one of those hidden operator. Dot you know what it looks like ?? Well the answer is in the question it self. ?? is our new operator.

?? is referred to as null coalescing operator. Its main task is to assign a default value to an object if it is null.

Let's start with an example:

string text = "hello world!" ; 
string test = text ?? "this is not the value";

The outcome of the above code will result in test = hello world! because text is not null.

string text = null; 
string test = text ?? "this was null";

The value of test = this was null. Since text is null, it will assign the default value after ?? operator. It works with any objects not just string.

For more information check out MSDN library.