Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ?? mean in C#? [duplicate]

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

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

int availableUnits = unitsInStock ?? 0;
like image 322
NoviceToDotNet Avatar asked Aug 31 '25 10:08

NoviceToDotNet


2 Answers

if (unitsInStock != null)
    availableUnits = unitsInStock;
else
    availableUnits = 0;
like image 85
Marco Mariani Avatar answered Sep 02 '25 23:09

Marco Mariani


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.

like image 20
Jackson Pope Avatar answered Sep 02 '25 23:09

Jackson Pope