C# 中的双问号等号 (??=) 是什么?

LCI*_*III 13 c# conditional-operator equals-operator

我经常看到这样的言论:

int? a = 5;

//...other code

a ??= 10;
Run Code Online (Sandbox Code Playgroud)

??=第二行是什么意思?我以前见过??用于空合并,但我从未见过它与等号一起使用。

LCI*_*III 37

和这个是一样的:

if (a == null) {
  a = 10;
}
Run Code Online (Sandbox Code Playgroud)

这是 C# 8.0 中引入的运算符:https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-coalescing-operator

仅当左侧操作数的计算结果为 null 时,空合并赋值运算符 ??= 在 C# 8.0 及更高版本中可用,才会将右侧操作数的值分配给左侧操作数。如果左侧操作数的计算结果为非空,则 ??= 运算符不会计算其右侧操作数。