Raising events by calling delegates - why not name the event?

I am looking at an old piece of code and cannot somehow figure out the following:

public event EventHandler NameChanged;
#endregion

#region protected void OnNameChanged(EventArgs args)
/// <summary>
/// Raises NameChanged event.
/// </summary>
/// <param name="args">Event arguments.</param>
protected void OnNameChanged(EventArgs args)
{
    EventHandler eh = this.NameChanged;
    if (eh != null)
    {
        eh(this, args);
    }
}

Why is the event triggered by a delegate call? Can I not just name the event myself (NameChanged), as usual?

EDIT: I see that this is also offered on MSDN: https://docs.microsoft.com/en-us/dotnet/standard/events/

+6
source share
1 answer

Whenever you refer to an event, you are actually copying the call list to a local link. By doing so, you will make sure that between the validation of the event eh != nulland the call of the event, the eh(this, args)value of eh has not changed (possibly from another thread).

# 6 https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-conditional-operators

:

NameChanged?.Invoke(this, args);
+3

All Articles