Why sender type is null when dealing with events

From C # via CLR:

Note. Many people wonder why an event template requires a sender. The parameter must always be of type Object. After all, since MailManager will be the only type that raises an event from a NewMail EventArgs object, it makes more sense for the callback method for prototyping as follows:

void MethodName(MailManager sender, NewMailEventArgs e);

The template requires a sender. The parameter must be of type Object mainly due to inheritance. What if May lManager was used as the base class for SmtpMailManager? In this case, the callback method must have a sender parameter prototyped as SmtpMailManager instead of Mail Manager, but this cannot happen because SmtpMai lManager just inherited the NewMai Event l So, the code that was expecting the SmtpMail Manager to raise the event will still have an argument sender for SmtpMailManager In other words, cast is still required, so the sender parameter can also be entered as an object.

The next reason for entering the sender parameter as an object is simply fexibility. This allows the delegate to be used with several types that offer an event that passes NewMail. EventArgs object. For example, the PopMai lManager class can use the delegate even if this class was not from Mail Manager

I just can't understand why the sender is an object. Why can't it be generalized? so most of the time we don’t need to do generic casts

+5
source share
2 answers

In C # version 1 there were no generics.

+7
source

, . MailManager this.EventName<MailManager>(this, args), , .

, , CLR, . Javascript # #, : p

+3

All Articles