开发者

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

开发者 https://www.devze.com 2023-01-25 07:53 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicate: What do two question marks together mean in C#?
This question already has answers here: Closed 12 years ago.

Possible Duplicate:

What do two question marks together mean in C#?

What does the ?? 开发者_高级运维mean in this C# statement?

int availableUnits = unitsInStock ?? 0;


if (unitsInStock != null)
    availableUnits = unitsInStock;
else
    availableUnits = 0;


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.


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

?? Operator (C# Reference)


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号