Thursday 9 November 2017

?? in C# (double question mark operator in C#) Or null coalescing operator

null coalescing operator


The operator "??", yes two question marks. called as  "null-coalescing" or just the double question mark operator.

According to the Microsoft, help page: "The ?? operator returns the left-hand operand if it is not null, or else it returns the right operand."

eg : var result = answer1 ?? answer2;
            can also be written as below
var result = answer1 != null ? answer1 : answer2;

if answer1 is not null =>
     result = answer1

if answer1 is null =>
   result = answer2

?? will be better uasable when we have to check for multiple null values for a result

string result = answer1 ?? answer2 ?? answer3 ?? answer4;
For more information please check
https://stackoverflow.com/questions/446835/what-do-two-question-marks-together-mean-in-c