Click here to Skip to main content
15,440,964 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Off and on for three years, I've tried and tried, but I cannot wrap my head around why a delegate is useful. Ignoring Events for the moment, and possibly even the ability to add multiple methods to a delegate (provided they all have the same signature), why would you code:

public delegate void SomeDelegate(int x);
SomeDelegate sd = SomeDelegate(SomeMethod);
sd(24);


Instead of:

SomeMethod(24);


I don't get it. I've read articles, books, and I must be missing something relevant because it seems like an awful waste of code.

Thanks in advance!

[edit]Shouty title and text, code block fixed. - OriginalGriff[/edit]
Posted
Updated 8-Nov-10 5:26am
v2

I wrote a tip/trick about one way to use delegates that doesn't have anything to do with events.

Delegates Are Good, and Good For You[^]

You also use delegates to communicate with the UI thread from other threads.
 
Share this answer
 
 
Share this answer
 
Comments
EngleA 8-Nov-10 15:56pm    
Those are the very articles which prompted my post. ;)
Ed Guzman 8-Nov-10 16:04pm     CRLF
:-) ok. One of the simplest implementation of delegates: you want to use one variable and dynamically changed its behavior based on the logic of your program. This variable has not value, it has functionality. And you can change the functionality on the fly, just re-assigning different function into this variable. Good luck.
My answer - "why do we need pointers to functions"?
 
Share this answer
 
v2
Comments
EngleA 8-Nov-10 12:26pm    
Dammit Jim, I'm a web developer! heh...
Toli Cuturicu 8-Nov-10 12:57pm    
Then add the Web tag to your question's tag list!
A similar question was asked a while ago. Have a look and the answer (and much more) can be found there.

PLZ HELP ME EXPLAINING[^]

Good luck!
 
Share this answer
 
Comments
EngleA 8-Nov-10 11:55am     CRLF
That was no help. I said, IGNORING events (as the link you posted is talking about button events), help me understand. Please keep answers simple. That post of PLZ HELP ME EXPLAINING was convoluted, full of inexplicable code, and completely irrelevant. Thanks!
E.F. Nijboer 8-Nov-10 16:59pm     CRLF
Events are just delegates that are part of the user interface. If you have another class, like List, you can execute a find with a specific compare function. This is very useful because it gives the option for custom comparing, something the writers of that class couldn't know at time of writing. http://msdn.microsoft.com/en-us/library/fh1w7y8z.aspx
Hi,

A quick and simple answer would be that it provides a thread-safe way of accessing methods between classes. For example in the case of a Winforms Application, accessing a progress counting method whilst displaying that progress in real time in the application window.

As Winforms Applications are displayed using the STA Thread, any thread blocking process called from another class would cause the application to either crash or not display the correct information.

I might be wrong there and accept any criticism.

Hope that helps.

Larrythemule
 
Share this answer
 
Comments
EngleA 8-Nov-10 12:13pm    
That makes sense. What do you think though in the ASP.NET world?
Chris Trelawny-Ross 8-Nov-10 13:20pm    
I regret to say that delegates don't actually help make your code thread safe, but they do make it type safe. A delegate is an object (can be passed as a variable into a method, can be assigned to a property, etc.) that encapsulates a method - but as well as encapsulating the method it encapsulates the signature of the method. This prevents a method that, say, takes a string as its paramter being used where a method that takes a List is required.
Laurence1234 8-Nov-10 14:01pm     CRLF
EngleA - Sorry I don't really have much idea on that I'm afraid. Chris - Ah I see. Sorry for the incorrect post. I think the confusion has arisen for me as I have had exceptions thrown before relating to Threads and have fixed it using Delegates. Could you give an example do you think? Also I'm not sure I follow your last sentence, as if you tried to pass the wrong parameters to a method, it would not compile.
We use delegates to manage calls to the database. We have an exception handler that can manage certain types of server errors and perhaps retry firing some methods, maybe if it was just a timeout, for example.

With delegates, it's easy because we don't need to know the method/signature; we just re-fire the delegate if we can attempt the call again in the service layer without having any knowledge of the context in which we're firing the call.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900