Why do we need the “event” keyword while defining events?

后端 未结 6 1689
忘了有多久
忘了有多久 2020-11-29 17:43

I don\'t understand why do we need the \"event\" keyword while defining events, when we can do the same thing without using \"event\" keyword, just by using the delegates.

6条回答
  •  渐次进展
    2020-11-29 18:19

    Events have distinct advantages compared to delegate-fields. Events can be defined in interfaces in contrast to fields, adding abstraction to the code, and even more importantly: Events can only be called from inside the defining class. In your case, anybody could call the event, possibly destroying your code.

    See this blog post for further information.

提交回复
热议问题