I find it very strange after spending quite a reasonable time of my life programming using C#, I have just come across this operator which is very useful, easy to work with and handy to make your code look as clean as you could wish for. THE DOUBLE QUESTION MARK (??) OPERATOR known as coalesce operator. An operator which is very useful while working with nullable types.
Let say we have two nullable integers:
int? nullableOne = null;
int? nullableTwo = 20;
What if you have been asked to provide a non-null value and if both the values are null then return default as 30? Immediately the following two solutions come to your mind:
if(nullableOne != null)
if(nullableTwo != null)
return (nullableOne != null ? nullableOne : (nullableTwo != null ? nullableTwo : 30) );
but with coalesce operator the expression could be:
return (nullableOne ?? nullableTwo ?? 30);
This expression will return the first value that is not null.