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

后端 未结 2 1671
说谎
说谎 2020-12-04 02:10

Can someone please explain to me what does the question mark in the member access in the following code means?

Is it part of standard C#? I get parse errors when try

相关标签:
2条回答
  • 2020-12-04 02:47

    It is Null Propagation operator introduced in C# 6, it will call the method only if object this.AnalyzerLoadFailed is not null:

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

    is equal to :

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

    See C# 6.0 – Null Propagation Operator , also you can see here

    i also once wrote about this upcoming feature in c# 6 here

    0 讨论(0)
  • 2020-12-04 03:00

    In C# version 6 it will be shorthand for

    if (this.AnalyzerLoadFailed != null)
        this.AnalyzerLoadFailed.Invoke(this, new AnalyzerLoadFailureEventArgs(AnalyzerLoadFailureEventArgs.FailureErrorCode.NoAnalyzers, null, null));
    
    0 讨论(0)
提交回复
热议问题