The C# ?? operator

The ?? operator C# operator returns the left side if it is not null. It returns the right side if the left side is null. It is very handy in situations where you can expect a value to be null but you want to use a default value if it is. Here is an example:

        string valueCouldBeNull = GetTheValue() ?? "The default value";
        Console.WriteLine("The result is " + valueCouldBeNull);
 

When assigning nullable types to non-nullable types you can avoid compile-time exceptions using the ?? operator.

kick it on DotNetKicks.com