Why should events in C# take (sender, EventArgs)?

前端 未结 10 1285
轻奢々
轻奢々 2020-12-08 18:46

It\'s known that you should declare events that take as parameters (object sender, EventArgs args). Why?

10条回答
  •  我在风中等你
    2020-12-08 19:35

    "Object sender" allows to reuse one method for multiple objects when the handler method is supposed to do something with the object that raised the event, for example 3 textbox can have one single handler that will reset the text of the firing textbox.

    EventArgs's main advantage is that it allows to refactor event information without the need to change signatures of all handlers in all projects that are subscribed to this kind of event.

    I can't think of a smarter way to deal with events.

提交回复
热议问题