What are the benefits/advantages of using delegates? Can anyone provide any simple examples?
What are the benefits/advantages of using delegates? Can anyone provide any simple examples?
Share
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.
They’re a great way of encapsulating a piece of code. For instance, when you attach an event handler to the button, that handler is a delegate. The button doesn’t need to know what it does, just how to call it at the right time.
Another example is LINQ – filtering, projecting etc all require the same kind of template code; all that changes is the logic to represent the filter, the projection etc. With lambda expressions in C# 3 (which are converted into delegates or expression trees) this makes it really simple:
(That can also be represented as a query expression, but let’s not stray too far from delegates.)
Another way of thinking of a delegate is as a single-method interface type. For example, the
EventHandlerdelegate type is a bit like:But the delegate support in the framework allows delegates to be chained together, invoked asynchronously, used as event handlers etc.
For more on delegates and events, see my article on the topic. Its focus is events, but it covers delegates too.