11

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 trying to compile this file in Xamarin Studio.

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

AnalyzerFileReference.cs line 195

Wickoo
  • 5,872
  • 4
  • 30
  • 44
  • 3
    It will be part of C# version 6. See http://damieng.com/blog/2013/12/09/probable-c-6-0-features-illustrated, "7. Monadic null checking" – Hans Kesting Oct 01 '14 at 13:29

2 Answers2

26

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

Ehsan Sajjad
  • 60,736
  • 15
  • 100
  • 154
  • 3
    It's bit older answer, but the above isn't correct. It's equal of the **threadsafe** version of the above code, which involves to assign the event handler to a local variable and doing the check on the local variable – Tseng Mar 16 '15 at 15:42
6

In C# version 6 it will be shorthand for

if (this.AnalyzerLoadFailed != null)
    this.AnalyzerLoadFailed.Invoke(this, new AnalyzerLoadFailureEventArgs(AnalyzerLoadFailureEventArgs.FailureErrorCode.NoAnalyzers, null, null));
Andrey Korneyev
  • 25,929
  • 15
  • 67
  • 67