Another use for the null-condition member access is invoking delegates in a thread-safe way with much less code. The old way requires code like the following:
var handler = this.PropertyChanged;
if (handler != null)
handler(…)
The new way is much simpler:
PropertyChanged?.Invoke(e)
The new way is thread-safe because the compiler generates code to evaluate PropertyChanged
one time only, keeping the result in temporary variable.
You need to explicitly call the Invoke
method because there is no null-conditional delegate invocation syntax PropertyChanged?(e)
. There were too many ambiguous parsing situations to allow it.