What does a double question mark do in C#? [duplicate]

狂风中的少年 提交于 2021-02-04 09:32:25

问题


Possible Duplicates:
?? Null Coalescing Operator --> What does coalescing mean?
What do two question marks together mean in C#?

I couldn't find this question being asked here so I figured I would ask it. What does a double question mark do in C#?

Example:

x = y ?? z;

回答1:


This is a null coalescing operator. The method above states x is assigned y's value, unless y is null, in which case it is assigned z's value.




回答2:


From Wikipedia:

It's the null-coalesce operator and shorthand for this:

x = (y != null ? y : z);



回答3:


Use y if not null, otherwise use z.




回答4:


If a the value y is null then the value z is assigned.

For example:

x = Person.Name ?? "No Name";

If name is null x will have the value "No Name"




回答5:


If y is null x will be set to z.




回答6:


.Net framework 2.0 onwards allow null values to Nullable value types.

here in this case, it says x equals y if it has some value (ie not null) or else equals z




回答7:


As others have stated, it is the null coalescing operator.

MSDN information on this:

https://docs.microsoft.com/dotnet/csharp/language-reference/operators/null-coalescing-operator



来源:https://stackoverflow.com/questions/1608174/what-does-a-double-question-mark-do-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!