Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why to not use a custom class instead of inheriting the EventArgs class

I'm wondering why should I use a class that inherits from the EventArgs class instead of using a custom class that will do the same job for me when passing event data?

like image 598
Yair Nevet Avatar asked Jul 25 '11 13:07

Yair Nevet


People also ask

Which class should be inherited when a class for custom event argument is written?

AssemblyLoadEventArgs class derives from EventArgs and is used to hold the data for assembly load events. To create a custom event data class, create a class that derives from the EventArgs class and provide the properties to store the necessary data. The name of your custom event data class should end with EventArgs .

What does EventArgs mean?

EventArgs e is a parameter called e that contains the event data, see the EventArgs MSDN page for more information. Object Sender is a parameter called Sender that contains a reference to the control/object that raised the event. Event Arg Class: http://msdn.microsoft.com/en-us/library/system.eventargs.aspx.


3 Answers

You don't have to inherit from EventArgs, but it allows people using your classes to use and handle generic *Handler(object sender, EventArgs e) declarations. If you don't inherit from EventArgs, then they have to use explicitly typed

*Handler(object sender, YairsFakeEventArgs e) 

The same goes for just using custom delegates but they are a lot more explicitly different.

like image 153
Deanna Avatar answered Sep 25 '22 16:09

Deanna


Because your event-args class will be compatible with any other function that accepts an EventArgs object.

like image 20
Fredrik Mörk Avatar answered Sep 23 '22 16:09

Fredrik Mörk


Because that's what would be the standard and most idiomatic way to do this in .NET. The whole ASP.NET and WinForms event model relies on those conventions. If another developer reads your code he will more easily understand it as this is the standard. This being said you could of course use any class you like.

like image 31
Darin Dimitrov Avatar answered Sep 26 '22 16:09

Darin Dimitrov