简体   繁体   中英

.Net Events - What use (object sender, EventArgs e) parameters give in Events other than onClick

I did not find .NET Events - What are object sender & EventArgs e? post much helpful.

It primarily tells that how the parameters (object sender, EventArgs e) are used in case of onClick scenario, which turns out to be the obvious use.

My question is :

Neglecting the onClick function

ie In case of Page_Load, Init and other page events

What is the use of these (object sender, EventArgs e) parameters ?

Examples would be more helpful :-)

Well it's pretty simple:

object sender is the entity which emitted the event - in case of page_load i'm pretty sure it is the page itself, because the page is emitting the event (so this == sender should be true in the page). EventArgs e is boring (it's a base class), but other events bring more interesting classes which tell something about the event, so a onMouseDown-Event for example will bring data about the mouse position on screen. Other events might bring other data - but most times the Event itself and the sender are enough to get all data needed.

Using standard(similar) signature such as (sender, eventargs) is beneficial because

  • It's flexible and generic enough to server many scenarios. Acts as a standard template even for novices
  • Similar signature increases the familiarity with the patterns, helps in reducing learning time for beginners
  • Enables re-usability at both event delegate level as well as handler level.

Now, once you accept/agree some standards, you need to stick them (even though in certain cases, it may not make sense provided that there is no huge cost associated with). .NET Fx developers has decided on this standard event template and that what's you will see everywhere.

Let's take an example of Page_Load - this event is actually declared at Control level . So its possible that some code may choose to handle multiple control's load event using the same event handler and use sender argument to apply specific things.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM