Are these 2 samples the same? Can old-style raising be replaced with Invoke and null propagation?
OLD:
public event EventHandler<MyEventArgs> MyEvent;
protected virtual void OnMyEvent(MyEventArgs args)
{
EventHandler<MyEventArgs> handler = this.MyEvent;
if (handler != null)
handler(this, args);
}
NEW:
public event EventHandler<MyEventArgs> MyEvent;
protected virtual void OnMyEvent(MyEventArgs args)
{
this.MyEvent?.Invoke(this, args);
}
Null check is important but it is clear. What is about additional variable?
How does null-propogation work internally? Is it thread-safe with events?
P.S. Regarding thread safety in events you can read here:
C# Events and Thread Safety