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

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

    Sometimes you would like to force all of your event consumers to use a particular event parameter, for example, a security event which passes a boolean parameter, true for good, false for bad. In this case you want your consumer to be painfully aware of the new parameter, i.e. you want your consumers to be coupled with that parameter. Take note, your consumers are still decoupled from your event firing class, but not from your event.

    I suspect that this scenario applies to a large number of cases and in those cases the value of EventArgs is greatly reduced.

提交回复
热议问题