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

前端 未结 10 1208
轻奢々
轻奢々 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:40

    It is a good pattern to use, that way what ever implements the event can find what was sending it.

    Also overriding the EventArgs and passing data through them is the best method. The EventArgs are a base class. If you look at various controls that call events, they have overridden EventArgs which gives you more information about the event.

    Even if you don't need the arguments to do the event, if you do not include them with the first run of the framework and want to add them later, you break all previous implementations, and have to re-write them. Plus if you a creating a framework and going to distribute that it becomes worse because everybody that uses your framework will need to refactor.

提交回复
热议问题