What is the difference between event and delegate?

9

I have already understood how delegate and event works, but I have not seen event utility. For example:

public delegate void ChangedEventHandler(object sender, BaseEventArgs e);
public ChangedEventHandler Changed;

The code above works like the one below:

public delegate void ChangedEventHandler(object sender, BaseEventArgs e);
public event ChangedEventHandler Changed;

If both work in the same way, what is the utility of event ?

By what I understand (if I am wrong, correct me) event is a modifier that shows that it is instantiating a delegate that has as parameters a class derived from EventArgs and an object? And that event can also be placed on interfaces?

    
asked by anonymous 04.04.2015 / 17:37

1 answer

4

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:)

04.04.2015 / 17:49