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

有人可以向我解释下列代码中的成员访问中的问号是什么意思?

它是标准C#的一部分吗? 当试图在Xamarin Studio中编译这个文件时,我得到了解析错误。

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

AnalyzerFileReference.cs第195行


它是在C#6中引入的Null Propagation运算符 ,只有在对象this.AnalyzerLoadFailed不为null时它才会调用该方法:

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

等于:

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

请参阅C#6.0 - 空传播运算符 ,也可以在此处看到

我也曾在这里用c#6写过关于这个即将发布的功能


在C#版本6中,它将是速记

if (this.AnalyzerLoadFailed != null)
    this.AnalyzerLoadFailed.Invoke(this, new AnalyzerLoadFailureEventArgs(AnalyzerLoadFailureEventArgs.FailureErrorCode.NoAnalyzers, null, null));
链接地址: http://www.djcxy.com/p/53857.html

上一篇: What does the question mark in member access mean in C#?

下一篇: What does the @ symbol before a variable name mean in C#?