The classic general C# event has these parameters:
(object sender, EventArgs e)
I can implement an event with a more specific signature for the e
argument, deriving for EventArgs
.
Now, what's the purpose of a base class like EventArgs
? I mean... it's empty. No base/abstract/virtual properties, nor fields, or something else.
Why the parameters of a basic event aren't just like below?
(object sender, object eventArgs)
That is, why all the event with some implemented and specific event-args parameter derive it from EventArgs
and not from a simple object
?
The above question is mirrored with the following one. The event delegate in the generic form is:
delegate void EventHandler<TEventArgs>(object sender, TEventArgs e)
and no restrictions are put on the parameter e
. But I would have expected something like where TEventArgs : EventArgs
, to be coherent...
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.
EventArgs is the base class of all event arguments and doesn't say much about the event. Several events use a derived class to supply more data, eg. a KeyPress event uses the KeyEventArgs class which contains the actual key pressed in its KeyChar property.
EventArgs is a base class for conveying information for an event. For reusability, the EventArgs subclass is named according to the information it contains rather than the event for which it well be used.
Events enable a class or object to notify other classes or objects when something of interest occurs. The class that sends (or raises) the event is called the publisher and the classes that receive (or handle) the event are called subscribers.
Object wouldn't preclude value types like int, double, etc. Which would introduce boxing and un-boxing issues. The choice of using a base class over object is a choice to enforce the passing of strongly typed objects throughout an API.
I tend to cringe when I see pervasive use of the object type as it kind of defeats the whole point of using a strongly typed programming language, you might as well go program in javascript, although anyone remotely familiar with javascript will know they are striving towards a strongly typed programming paradigm.
EDIT: To elaborate further on the distinction between an event model passing reference types vs value types. When a delegate handling an event alters the data for an event, which many people frequently do when raising an event, if the data passed were a value type you would then need to start thinking about whether you're changing a copy passed by value or the original reference to the value type, of course you would hope you are altering the original. Enforcing the passing of reference types is a pretty critical design decision in the .NET event model.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With