When use a delegate?

C

Cirene

When would you choose to use a delegate rather than calling a function
directly? What's the advantages? Thanks...
 
C

Cowboy \(Gregory A. Beamer\)

One prime example is events.

Another is when you have to run more than one "function" when a specific
action happens.

Another is when you have a library that has to make a callback to the
calling application, but you do not know what the event name is at the time
you create the library.
 
S

Scott M.

Sometimes you don't know what function you're going to want to call, only
its signature. Delegates allow you that flexibility.
 
J

Jordan S.

Cirene said:
When would you choose to use a delegate rather than calling a function
directly? What's the advantages? Thanks...


Here is a link to a good article that presents an introduction to
delegates - to the extent that delegates are relevant to events.

http://www.codeproject.com/KB/cs/event_fundamentals.aspx#3.2WhyDelegates4

While the article is about events, one must have a good understanding of
delegates in order to really understand events in C#. So much of the article
addresses delegates.

-HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,945
Members
47,495
Latest member
Jack William

Latest Threads

Top