Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 249
Mike Rosenblum Avatar asked Aug 23 '09 13:08

Mike Rosenblum


People also ask

What is the use of EventArgs EventArgs?

EventArgs EventArgs EventArgs EventArgs Class. Definition. Represents the base class for classes that contain event data, and provides a value to use for events that do not include event data.

What are the different types of event ARGs systems?

Safe Serialization Event Args System. Service Model. Discovery. Announcement Event Args System. Service Model. Security. WSTrust Request Processing Error Event Args System. Service Model. Unknown Message Received Event Args System. Speech. Recognition. Audio Level Updated Event Args System. Speech.

Is eventwithoutparams() the best way to fire an event?

Smart idea, have no clue why people want to do all of the unnecessary work of events only to waste their advantages! Unfortunately, no, simply calling EventWithoutParams () is not the best way to fire the event. The reason, as Eric Lippert points out, is that you can end up with a NullReferenceException.

What is an event in coding example?

Event Coding An event is a kind of ‘Something Happened’. Some examples are the button got pressed; a checkmark from the check box is removed. We call these kinds of actions Events. So let us consider a form that has a button in it. We all know that a button can be clicked.


1 Answers

From Framework Design Guidelines by Brad Abrams and Krzysztof Cwalina:

Consider using a subclass of EventArgs as the event argument, unless you are absolutely sure the event will never need to carry any data to then event handling method, in which case you can use the EventArgs type directly.

If you ship an API using EventArgs directly, you will never be able to add any data to be carried with the event without breaking compatibility. If you use a subclass, even if initially completely empty, you will be able to add properties to the subclass when needed.

Personally, I think this comes down to the compatibility issue. If you are making a class library to be consumed by others, then I would use a subclass. If you are only using the events in your own code, then (as Alfred has said), YAGNI applies. It would be a breaking change when you change from EventArgs to your own derived class, but since it would only break your own code it is not too much of a problem.

like image 165
adrianbanks Avatar answered Nov 03 '22 22:11

adrianbanks