What are the advantages of adding delegate instances to Component.Events (EventHandlerList)?

Viewed 1073

In class's derived from a Component I sometimes see events declared like:

private static readonly object LoadEvent = new object();
public event EventHandler<MyEventArgs> Load
{
    add { Events.AddHandler(LoadEvent, value); }
    remove { Events.RemoveHandler(LoadEvent, value); }
}

protected virtual void OnLoad(MyEventArg e)
{
    var evnt = (EventHandler<MyEventArg>)Events[LoadEvent];
    if (evnt != null) 
        evnt(this, e);
}

Instead of just:

public event EventHandler<MyEventArgs> Load;

protected virtual void OnLoad(MyEvent e)
{
   if (Load != null)
       Load(this, e);
}

I'm tempted to to refactor to use the shorter method, but I am hesitant in case there are some advantages to using the Component EventHanderList that I am missing.

The only advantages I can currently think of are:

  • When the component is disposed, all items in the EventHandlerList are removed, effectively automatically unhooking event handlers.
  • Possibly less memory fragmentation because of all the attached delegates going into the single EventHandlerList.

Is there anything else?

(This is not a question about the general use of explicit add + removes on events.)

1 Answers
Related