Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the purpose of EventArgs as base class in the event pattern?

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...

like image 315
Massimiliano Kraus Avatar asked May 16 '17 21:05

Massimiliano Kraus


People also ask

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.

What is EventArgs in VB?

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.

What base class do you use to convey information for an event?

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.

What is event in C sharp?

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.


1 Answers

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.

like image 126
Mick Avatar answered Sep 20 '22 14:09

Mick