21

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?

Yair Nevet
  • 12,725
  • 14
  • 66
  • 108

3 Answers3

28

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.

Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
Deanna
  • 23,876
  • 7
  • 71
  • 156
18

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

Fredrik Mörk
  • 155,851
  • 29
  • 291
  • 343
6

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.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928