Don't understand delegates

  • Thread starter Thread starter Tem
  • Start date Start date
T

Tem

I've read every example i could find on the subject and still couldn't
figure out its proper usage.

What's the point of delegates, why can't I just invoke the method
directly???

Can someone please help?

Tem
 
Delegates are pointers to methods. They are a way of having a method as a
variable which can be passed around. They are handy for dependency injection
and things. For example. I have some classes which initialise my business
layer at the start of my application. They take a Delegate to a method like
void ReportStatus(string)
The function call this delegate passing a string which states what they are
currently doing for initialisation. This allows be to change how they report
their information by passing in different methods.
The windows forms app passes in a reference to a method which updates a
label on the splash screen. The command line version passes in a method with
prints to the command line. and potentially a server version could pass one
in which logs to a file/database etc and if i dont want to log at all i can
pass a null delegate
The good thing about this is that the logic for logging is seperated
entirely from the logic for initialisation so they are changeable and
testable independantly.

Does this help you see the usefullness of them?
 
This is effectively how events work. In a normal situation like the below,
you could use an event which is effectively a list of delegates to call when
the event is raised. There is a reason in the below example why it isnt an
event but its not important for this discussion.
 
I've read every example i could find on the subject and still couldn't
figure out its proper usage.

What's the point of delegates, why can't I just invoke the method
directly???

Because maybe you do not know which exact method to call. Or maybe the
exact method to call change dynamically.

One way of think of it as if your class is incomplete and is the code
using it who is going to complete it.
 
Ignacio points out the primary reason: You are not sure what you are calling
until runtime.

Delegates are aslo useful for event handlers, when you only want a specific
bit of code called in a certain instance.

Another reason to delegate is callbacks, which cannot be coded directly as
method calls. I actually restated reason #2, only in different language, and
reason #1, at least in some instances. :-)

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

Subscribe to my blog
http://gregorybeamer.spaces.live.com/lists/feed.rss

or just read it:
http://gregorybeamer.spaces.live.com/

*************************************************
| Think outside the box!
|
*************************************************
 

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

Back
Top