The Microsoft documentation on events includes the following example:
class Counter
{
public event EventHandler ThresholdReached;
protected virtual void OnThresholdReached(EventArgs e)
{
EventHandler handler = ThresholdReached; // <-- (*)
handler?.Invoke(this, e);
}
// provide remaining implementation for the class
}
You can find it here: https://docs.microsoft.com/en-us/dotnet/standard/events/
Whats the motivation behind the instantiation at (*), why not just write:
class Counter
{
public event EventHandler ThresholdReached;
protected virtual void OnThresholdReached(EventArgs e)
{
ThresholdReached?.Invoke(this, e);
}
}