(All this from the point of view of C #.)
I have an article on the differences between events and delegates . This describes in more detail everything below.
Basically, I like to think that an event is like a property - it's a couple of methods, that's all. Instead of get / set, the event has add / remove, which means "add an event handler" and "remove this event handler." At the heart of everything is all events.
C # also has field-like events, which are shortcuts:
public event EventHandler Foo;
declares both a field and an event with an almost trivial add / remove implementation. In a class related to Foo , refers to a field. Outside the class, referring to Foo , refers to the event.
The main idea is that the event allows another code to subscribe and refuse it by passing a delegate (event handler). Typically, a subscription is implemented by creating a new multicast delegate containing the previous list of event handlers and a new one. Therefore, if you store event handlers in a field called myEventHandlers , the subscription implementation may be:
myEventHandlers += value;
Similarly, unsubscribing usually involves creating a new multicast delegate without the specified handler:
myEventHandlers -= value;
Then, when you want to raise / fire an event, you simply call this multicast delegate - usually with an invalidation check to throw an exception if no one has signed up:
EventHandler handler = myEventHandlers; if (handler != null) { // You could pass in a different "sender" and "args" of course handler(this, EventArgs.Empty); }
Using events, subscribers do not know about each other and cannot raise the event themselves (usually). In other words, this is an example of encapsulation, which has received status both in the language and on the platform.