Delegates look like such a powerful language feature, but I’ve yet to find an opportunity to use them in anger (apart from in DALs I must say).
How often do you use them, and under what circumstances do you find them most useful?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Funcs and Actions are newish ‘types’ of delegates and I use them a lot with Linq and really other odd situations. For Linq they are nice because personally I’d rather have a descriptive name than a lambda expression:
Where with a Func I can:
It might be a line more, but there are times where I find myself repeating a certain lambda expressions a couple times in a class, so personally I think Funcs work well for that.
Another situation is a, for lack of a better term, Method Factory. Basically it is a dictionary that holds a list of Actions as the value and say an Enum as the key:
And then I have the methods themselves written out somewhere else on the page. And use would be:
Or maybe you want to pass the method itself to another:
I’m sure there are much better uses for delegates, but these are useful.