The event system is a much more powerful mechanism. It uses delegates as a basis but it does more than that. It is the implementation in the language of what is called Observer Pattern
( implementation example in C # ).
Note that in the example shown in link above everything that needs to be done to have a working event needs to be done by the programmer. The event
command tells the compiler that it should be part of this "dirty work" for you. Not that it's too complicated, but it certainly gives more work and it's easier to make mistakes when you have more to worry about.
Nothing prevents you from implementing this design pattern on your own with or without delegates. But you're going to have to write all the code needed for this. You'll have to make sure he's all right. You'll need to write down the entire mechanism that manages the sign-up and shutdown mechanisms of the event, as well as the notification process.
The event abstracts the mechanism. It is a concept at the highest level. In a way, we can say that you're the same subject as the this question .
One way to see what an event does is like this:
private ChangedEventHandler ChangedField;
public void AddChangedHandler(ChangedEventHandler handler) {
this.ChangedField = (ChangedEventHandler)Delegate.Combine(this.Changed, handler);
}
public void RemoveChangedHandler(ChangedEventHandler handler) {
this.ChangedField = (ChangedEventHandler)Delegate.Remove(this.Changed, handler);
}
The compiler implements this for you. And it's not so simple to do right because it implements this through a MulticastDelegate
. Again, nothing prevents you from doing this and you may even need to do something more specific. It also gives more security by preventing certain operations from being done with the delegates. Without its use, it is possible to raise an event externally, it is possible to replace a delegate improperly.
No longer is your understanding correct. Do not expect too much magic in it. It does nothing that can not be done without it in a "worse" way.
If you do not care for the ease it gives you should at least use it to make it semantically clear that you are using an event and not just a delegate. It is also a question of intention. It is useful information for the programmer to better understand the code, allows the compiler to do something more, and allows other components to interact with it in a more specific way, as it knows best what these delegates are being used for and how they were implemented.
Try using both ways and get the CIL code generated by the compiler to see how it changes.
Not everyone knows that C # was born because of a disagreement between Microsoft and Sun - creator of Java - because of this feature. At least that's what the legend says. Lucky for us:)