Should an Event that has no arguments define its own custom EventArgs or simply use System.EventArgs

Posted by Mike Rosenblum on Stack Overflow See other posts from Stack Overflow or by Mike Rosenblum
Published on 2009-08-23T13:27:07Z Indexed on 2010/03/26 12:33 UTC
Read the original article Hit count: 494

Filed under:
|
|

I have an event that is currently defined with no event arguments. That is, the EventArgs it sends is EventArgs.Empty.

In this case, it is simplest to declare my Event handler as:

EventHandler<System.EventArgs> MyCustomEvent;

I do not plan on adding any event arguments to this event, but it is possible that any code could need to change in the future.

Therefore, I am leaning towards having all my events always create an empty event args type that inheretis from System.EventArgs, even if there are no event args currently needed. Something like this:

public class MyCustomEventArgs : EventArgs
{
}

And then my event definition becomes the following:

EventHandler<MyCustomEventArgs> MyCustomEvent;

So my question is this: is it better to define my own MyCustomEventArgs, even if it does not add anything beyond inheriting from System.EventArgs, so that event arguments could be added in the future more easily? Or is it better to explicitly define my event as returning System.EventArgs, so that it is clearer to the user that there are no extra event args?

I am leaning towards creating custom event arguments for all my events, even if the event arguments are empty. But I was wondering if others thought that making it clearer to the user that the event arguments are empty would be better?

Much thanks in advance,

Mike

© Stack Overflow or respective owner

Related posts about c#

Related posts about events