-3

I know ? checks for null when placed before a . member access and ?: for conditional statements. Although, I think ?? checks for null as well but I'm not very sure

I can't find useful information about ?? on https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/

PS. Actually I didn't look well at the MSDN reference very well. I've just seen its definition now.

I though of closing this post before but I won't for the sake of anyone who wouldn't think of referring to ?? as double question marksin their question

Community
  • 1
  • 1
tushortz
  • 3,697
  • 2
  • 18
  • 31

1 Answers1

2

that operator is sugarSyntax for operations with nullable operands

The ?? operator returns the left-hand operand if it is not null, or else it returns the right operand.

int? counter = null;

int backup = counter ?? 0;

in this case backup will be assigned with counter value IF counter is different to null, for something ELSE then backup will be assigned with 0

note that I bold the keywords IF-ELSE which make us infer that ?? operand can be replaced by simple old-school if else conditionals.

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97