?? Null-Coalescing Operator C# with Example



?? Null-Coalescing Operator C# with Example

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(); 

0 Comment's

Comment Form

Submit Comment