可能的复制品: ? ? Null 聚合算子—— > 聚合是什么意思? 两个问号一起在 C # 中意味着什么
我在这里找不到这个问题,所以我想我会问它。双重问号在 C # 中有什么作用?
例如:
x = y ?? z;
If y is null x will be set to z.
Use y if not null, otherwise use z.
y
null
z
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.
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"
From Wikipedia:
It's the null-coalesce operator and shorthand for this:
x = (y != null ? y : z);
As others have stated, it is the null coalescing operator.
MSDN information on this:
https://learn.microsoft.com/dotnet/csharp/language-reference/operators/null-coalescing-operator
.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