关于null:成员访问中的问号在C#中意味着什么?

What does the question mark in member access mean in C#?

有人能向我解释一下,会员进入以下代码的问号是什么意思吗?

它是标准C的一部分吗?尝试在Xamarin Studio中编译此文件时,会出现解析错误。

1
this.AnalyzerLoadFailed?.Invoke(this, new AnalyzerLoadFailureEventArgs(AnalyzerLoadFailureEventArgs.FailureErrorCode.NoAnalyzers, null, null));

分析仪参考.cs行195


它是C 6中引入的空传播运算符,只有当对象this.AnalyzerLoadFailed不为空时,它才会调用该方法:

1
this.AnalyzerLoadFailed?.Invoke(this, new AnalyzerLoadFailureEventArgs(AnalyzerLoadFailureEventArgs.FailureErrorCode.NoAnalyzers, null, null));

等于:

1
2
if( this.AnalyzerLoadFailed != null)
    this.AnalyzerLoadFailed.Invoke(this, new AnalyzerLoadFailureEventArgs(AnalyzerLoadFailureEventArgs.FailureErrorCode.NoAnalyzers, null, null));

参见C 6.0–空传播运算符,您也可以在这里看到

我也曾经在这里的C 6中写过这个即将到来的特性。


在C版本6中,它是

1
2
if (this.AnalyzerLoadFailed != null)
    this.AnalyzerLoadFailed.Invoke(this, new AnalyzerLoadFailureEventArgs(AnalyzerLoadFailureEventArgs.FailureErrorCode.NoAnalyzers, null, null));