Delegation, Event-Listeners

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

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.

thanks in advance

regards

mat
 
In both cases, you have the definition, the wiring and the handler.
Delegates, however, are a bit more flexible. If you do not require the
flexibility, or feel you ever will, stick with events. That would be my
suggestion. I am not sure I can list a pro/con for each as they would be
pretty much the same list.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 
mathon said:
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.

Richard
 
Richard Grimes said:
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.
 
Back
Top