Events

I am trying to grasp your explanation on events based on what I learned about them. Since the publisher can access an event exactly the same way it can accesses a delegate, are you trying to say that the publisher doesn’t know what the subscribers are subscribing to for both events and delegates? And, the main reason that it uses an event instead of a delegate is the subscriber can only access the delegate, contained inside of the event, by using the + += - -=event accessors to subscribe or unsubscribe to the event; So, if you used an delegate instead of an event the subscribers would have full access to the delegate and could change it; which would be very-very bad!

Yup everything you said is absolutely correct.

The publisher doesn’t know (and doesn’t want to know) what subscribers are listening to the event.
If you don’t add the “event” keyword then yup every other class can += and -= but they can also trigger the event(); or set it to something event = null;

Adding the event keyword prevents that, subscribers can only += and -=, they cannot trigger or set the event.

1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms