When I declare an event, I’ve been using the Action delegate or Action<T, ...> delegate if the event handler has parameters. I recently noticed the existence of the EventHandler delegate, which requires a type param that inherits EventArgs. The latter method requires that I create an additional class that exists only to encapsulate whatever parameters are passed with the event, which seems unnecessary, unless I’m missing something.
So, what are the reasons for using the EventArgs delegate when declaring an event? What does it buy me?
It buys you the ability to return event arguments, that’s all. If you have no need for that, then maybe you don’t need an
EventHandlerdelegate.From a larger perspective,
EventArgsis the “socially acceptable” way of getting data from one place to another using events. You can create custom delegates to do this, of course, butEventArgshas tooling support in Visual Studio, etc.