What is the purpose of EventArgs as a base class in an event template?

The classic C # generic event has the following parameters:

(object sender, EventArgs e)

      

I can implement an event with a more specific signature for the argument e

by getting for EventArgs

.

Now, what is the purpose of a base class like EventArgs

? I mean ... it's empty. No base / abstract / virtual properties, fields or anything else.

Why are the parameters of the underlying event not so similar to below?

(object sender, object eventArgs)

      

Is that why all events with some implemented and defined event-args parameter retrieve it from EventArgs

rather than simple object

?

The above issue is reflected as follows. Event delegate in general:

delegate void EventHandler<TEventArgs>(object sender, TEventArgs e)

      

and no restrictions are imposed on the parameter e

. But I would expect something like where TEventArgs : EventArgs

to be consistent ...

+3


source to share


1 answer


The object will not interfere with value types like int, double, etc., which will present boxing and non-boxing issues . Choosing to use a base class over an object is a choice to force strongly typed objects to traverse the entire API.

I tend to cringe when I see the ubiquitous use of an object type since it kind of defeats the whole purpose of using a strongly typed programming language, you can run the program in javascript as well, although anyone who is familiar with javascript will know they are aiming strictly typed programming paradigm .



EDIT: Further clarifies the difference between an event model passing reference types and value types . When a delegate handling an event modifies data for an event, which many people often do when raising an event, if the data passed was a value type, you would need to start thinking about whether you were modifying the copy passed by value or the original reference to the value type of course you hope to change the original. Enforcing reference types traversal is a rather critical design decision in the .NET event model.

+5


source







All Articles