C# Language Operators ?? Null-Coalescing Operator

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!


The Null-Coalescing operator ?? will return the left-hand side when not null. If it is null, it will return the right-hand side.

object foo = null;
object bar = new object();

var c = foo ?? bar;
//c will be bar since foo was null

The ?? operator can be chained which allows the removal of if checks.

//config will be the first non-null returned.
var config = RetrieveConfigOnMachine() ??
             RetrieveConfigFromService() ??
             new DefaultConfiguration();

Got any C# Language Question?