C# 中的双问号等号 (??=) 是什么?
What is the double question mark equals sign (??=) in C#?
我经常看到这样的陈述:
int? a = 5;
//...other code
a ??= 10;
第二行的??=
是什么意思?我以前见过 ??
用于空合并,但我从未见过它与等号一起使用。
同理:
if (a == null) {
a = 10;
}
这是 C# 8.0 中引入的运算符:https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-coalescing-operator
Available in C# 8.0 and later, the null-coalescing assignment operator ??= assigns the value of its right-hand operand to its left-hand operand only if the left-hand operand evaluates to null. The ??= operator doesn't evaluate its right-hand operand if the left-hand operand evaluates to non-null.
我经常看到这样的陈述:
int? a = 5;
//...other code
a ??= 10;
第二行的??=
是什么意思?我以前见过 ??
用于空合并,但我从未见过它与等号一起使用。
同理:
if (a == null) {
a = 10;
}
这是 C# 8.0 中引入的运算符:https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-coalescing-operator
Available in C# 8.0 and later, the null-coalescing assignment operator ??= assigns the value of its right-hand operand to its left-hand operand only if the left-hand operand evaluates to null. The ??= operator doesn't evaluate its right-hand operand if the left-hand operand evaluates to non-null.