Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rationale behind EventArgs class

Tags:

c#

.net

events

I'm learning events in C# and understand that the EventArgs class carries data about the event. But I am having difficulties understanding why EventArgs is necessary.

For instance, in this MSDN example, couldn't the WakeMeUp class read all of the necessary data (snoozePressed, nrings) from the fields of AlarmClock? If it can set them, why couldn't it also get them?

like image 643
ark Avatar asked Jul 17 '09 08:07

ark


1 Answers

The upsides with the EventArgs class are (as I see it) mainly these two:

  • You can add members to the EventArgs class withouth altering the signature of the event
  • You disconnect the information passed to the event handler from the object instance

It may even be that the information contained in the EventArgs is not exposed by the object raising the event.

Sure, in some cases it may seem like overkill, but I think that the power of sameness is a good thing here; if you know how one event works, you know how other events work as well.

like image 64
Fredrik Mörk Avatar answered Sep 28 '22 01:09

Fredrik Mörk