4

Possible Duplicate:
What do two question marks together mean in C#?

What does the ?? mean in this C# statement?

int availableUnits = unitsInStock ?? 0;
Community
  • 1
  • 1
NoviceToDotNet
  • 10,387
  • 36
  • 112
  • 166

5 Answers5

5
if (unitsInStock != null)
    availableUnits = unitsInStock;
else
    availableUnits = 0;
Marco Mariani
  • 13,556
  • 6
  • 39
  • 55
4

This is the null coalescing operator. It translates to: availableUnits equals unitsInStock unless unitsInStock equals null, in which case availableUnits equals 0.

It is used to change nullable types into value types.

Jackson Pope
  • 14,520
  • 6
  • 56
  • 80
2

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

?? Operator (C# Reference)

Bolu
  • 8,696
  • 4
  • 38
  • 70
1

according to MSDN, The ?? operator is called the null-coalescing operator and is used to define a default value for a nullable value types as well as reference types. It returns the left-hand operand if it is not null; otherwise it returns the right operand.

Check out
http://msdn.microsoft.com/en-us/library/ms173224.aspx

Singleton
  • 3,701
  • 3
  • 24
  • 37
1

it means the availableUnits variable will be == unitsInStock unless unitsInStock == 0, in which case availableUnits is null.

ItsPronounced
  • 5,475
  • 13
  • 47
  • 86