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
EventHandler<object, MyCustomEventArgs>
a typo that should beEventHandler<MyCustomEventArgs>
? If not, where does this class reside? – RinseEventHandler<System.EventArgs>
, which I've now corrected. The fact that the sender is an object is implicit when using theEventHandler<>
class. For another take on this, you might want to have a look at "Event Signature in .NET — Using a Strong Typed ‘Sender’?" (#1046516), but this alternative approach is non-standard, so you might not want to go this route as a beginner. It is interesting though, and it does work very well. – Ediva