Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the deal with delegates?

Tags:

c#

asp.net

vb.net

I understand delegates encapsulate method calls. However I'm having a hard time understanding their need. Why use delegates at all, what situations are they designed for?

like image 592
m.edmondson Avatar asked Sep 20 '10 11:09

m.edmondson


People also ask

Why do people struggle delegates?

Fear of failure can cause leaders to hold on to work and refuse to delegate. They feel that they need to take charge and deliver the work personally to make sure it is done to the right standard. Some leaders like to say: “If you want something done properly, do it yourself.”

What is the role of the delegate?

A delegate is a person selected to represent a group of people in some political assembly of the United States.

Why do managers fear delegates?

Other reasons why managers do not delegate as much as they could include: The belief that employees cannot do the job as well as the manager can. The belief that it takes less time to do the work than it takes to delegate the responsibility. Lack of trust in employees' motivation and commitment to quality.

What is an advantage to having delegates?

Benefits of Delegating Delegation of tasks to others offers the following benefits: Gives you the time and ability to focus on higher-level tasks. Gives others the ability to learn and develop new skills. Develops trust between workers and improves communication. Improves efficiency, productivity, and time management.


2 Answers

A delegate is basically a method pointer. A delegate let us create a reference variable, but instead of referring to an instance of a class, it refers to a method inside the class. It refers any method that has a return type and has same parameters as specified by that delegate. It's a very very useful aspect of event. For thorough reading I would suggest you to read the topic in Head First C# (by Andrew Stellman and Jennifer Greene). It beautifully explains the delegate topic as well as most concepts in .NET.

like image 150
sumit_programmer Avatar answered Oct 23 '22 14:10

sumit_programmer


Well, some common uses:

  • Event handlers (very common in UI code - "When the button is clicked, I want this code to execute")
  • Callbacks from asynchronous calls
  • Providing a thread (or the threadpool) with a new task to execute
  • Specifying LINQ projections/conditions etc

Don't think of them as encapsulating method calls. Think of them as encapsulating some arbitrary bit of behaviour/logic with a particular signature. The "method" part is somewhat irrelevant.

Another way of thinking of a delegate type is as a single-method interface. A good example of this is the IComparer<T> interface and its dual, the Comparison<T> delegate type. They represent the same basic idea; sometimes it's easier to express this as a delegate, and other times an interface makes life easier. (You can easily write code to convert between the two, of course.)

like image 8
Jon Skeet Avatar answered Oct 23 '22 14:10

Jon Skeet