3

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
  • 9,889
  • 32
  • 106
  • 163

5 Answers5

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,225
  • 6
  • 53
  • 80
4
if (unitsInStock != null)
    availableUnits = unitsInStock;
else
    availableUnits = 0;
Marco Mariani
  • 13,284
  • 6
  • 38
  • 55
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,586
  • 4
  • 37
  • 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,645
  • 3
  • 23
  • 37
1

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

ItsPronounced
  • 5,397
  • 12
  • 45
  • 82