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

后端 未结 6 1694
忘了有多久
忘了有多久 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:36

    The event keyword does 3 different things:

    1. You can define an event in an interface, even though you cannot define regular fields in interfaces.
    2. It changes the visibility of the = and () operators (assignment and invocation) to private, so that only the containing class can invoke the event or override all the methods contained in it. The -= and += operators can still be invoked on an event from outside the class defining it (they get the access modifier you wrote next to the event).
    3. You can also override the way -= and += behave on events.

提交回复
热议问题