Richard Grimes <(E-Mail Removed)> wrote:
> mathon wrote:
> > could anybody explain me the advantages and disadvantages of
> > delegation and the advantages and disadvantages using
> > event-listeners? - both related to the treatment of events.
>
> Delegates are managed function pointer objects. Events are simply
> metadata. When you add a delegate to a class you have the responsibility
> to also add code to initialize the delegate and to invoke it in a thread
> safe way (and only if it is initialized). If you add an event to your
> class the compiler will add the delegate field, the methods to
> initialize it and the code to invoke it. Since the (Microsoft) compiler
> adds this stuff, it will add this code using set conventions so that
> other code that uses the event will know exactly how to use it.
Note that the compiler only adds the delegate field if you add a
"field-like" event. If you provide your own add/remove methods, the
compiler doesn't add any extra bits. The event itself is distinct from
the delegate - various classes in the framework don't use the "one
delegate field per event" model, having a map of them, for instance.
--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog:
http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too