There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.
If this question can be reworded to fit the rules in the help center, please edit the question.

13

out of curiousity, what did you respond so we can tell you how you can correct it?
–
Anthony ForloneyJan 11 '10 at 19:28

14 Answers
14

I like to think of a delegate as "a pointer to a function". This goes back to C days, but the idea still holds.

The idea is that you need to be able to invoke a piece of code, but that piece of code you're going to invoke isn't known until runtime. So you use a "delegate" for that purpose. Delegates come in handy for things like event handlers, and such, where you do different things based on different events, for example.

In C#, for example, let's say we had a calculation we wanted to do and we wanted to use a different calculation method which we don't know until runtime. So we might have a couple calculation methods like this:

+1 for the nod to the simple but effective function pointer in C.
–
Aiden BellJan 11 '10 at 19:28

1

One question related to your answer. How is it really different from calling a function in normal way? Just because of that it is not known at runtime ?
–
NAVEEDJan 11 '10 at 19:51

@NAVEED - refer to my latest edit, I included an example. As far as the actual method call, it doesn't look any different from a normal method call in my example above (the calcTotal(amt) is calling the delegate), but the power of delegates is that you can use them as parameters, etc. when you want a method to be able to have different behavior. There are many other things you can use them for also, this is just a simple example. Hope that helps.
–
dcpJan 11 '10 at 20:10

It's not known at runtime, and is a bound function rather than a free function - assigning a non-static method Foo to a delegate will call this.Foo() rather than a static function as a function pointer would do ( in C, you often have an extra void* parameter to pass this to the function pointer )
–
Pete KirkhamJan 11 '10 at 20:13

1

+1 for the quick and effective example establishing similarities with function pointers in C/C++. Much appreciated!
–
G21Sep 25 '13 at 20:25

Q What are delegates?A When an object receives a request, the object can either handle the request itself or pass the request on to a second object to do the work. If the object decides to pass the request on, you say that the object has forwarded responsibility for handling the request to the second object.

Or, as an easy pseudo example: something sends a request to object1. object1 then forwards the request and itself to object2 -- the delegate. object2 processes the request and does some work. (note: link above gives good examples)

This is not an example of delegation. In your example, you're using what's called a variable function which basically looks for a function having the same name as the string to which a variable refers. See variable functions: php.net/manual/en/functions.variable-functions.php
–
AquarelleAug 27 '13 at 4:46

In Event communication sender does not know which object will handle the event.
Delegate is type which hold the reference of method.
Delegate has signature and holds reference to method which matches its signature
so Delegate is like type safe function pointer.

button1.Click += new System.EventHandler(button1_Click)
System.EventHandler is declared as a delegate here
In .net Events work on the concept of Delegate (like Button Click)

Delegate is used when you do not know which code to invoke at run time
So at that time Delegate is used to handle Events

A delegate is an object that can refer to a method. Thus, when we create a delegate, we are creating an object that can hold a reference to a method. Furthermore, the method can be called through this reference. Thus, a delegate can invoke the method to which it refers.
The principal advantage of a delegate is that it allows us to specify a call to a method, but the method actually invoked is determined at runtime, not at compile time.

Here I am going to explain delegates, multicast delegates and their usage..
Delegate is a type which holds the method(s) reference in an object. It is also referred to as a type safe function pointer. We can say a delegate is a type that defines a method signature.

When you instantiate a delegate, you can associate its instance with any method with a compatible signature. You can invoke (or call) the method through the delegate instance.
Delegates are used to pass methods as arguments to other methods.
Event handlers are nothing more than methods that are invoked through delegates.
Advantages of using delegates are,
Encapsulating the method's call from caller
Effective use of delegate improves the performance of application
Used to call a method asynchronously.
There are some properties of delegates are

Delegates are like C++ function pointers but are type safe.
Delegates allow methods to be passed as parameters.
Delegates can be used to define callback methods.
Delegates can be chained together; for example, multiple methods can be called on a single event.
Methods do not have to match the delegate signature exactly.

public delegate type_of_delegate delegate_name() // Declaration

You can use delegates without parameters or with parameter list
If you are referring to the method with some data type then the delegate which you are declaring should be in the same format. This is why it is referred to as type safe function pointer. Here I am giving an example with String.

Here Delegate is added using the += operator and removed using the -= operator.

Delegate types are derived from the Delegate class in the .NET Framework. Delegate types are sealed—they cannot be derived.
Because the instantiated delegate is an object, it can be passed as a parameter, or assigned to a property. This allows a method to accept a delegate as a parameter, and call the delegate at some later time. This is known as an asynchronous callback.

You do not want to execute a piece of code at the time when you run the program.
After running the program you want to execute that piece of code whenever an event occurs.

Example :

Console application - code can be executed only at the time you run the program. (Written inside Main method)

Windows application (user interface programming ) - code can be executed when clicking the button after running the program.

This is what they say, you do not know which method will invoke at compiling time. you know it only at runtime that is when clicking the button.

Without delegates no user interface programming is possible. Because you are executing code whenever the user makes events that is clicking button , typing in textbox, selecting dropdownlist item and so on....

A delegate is something to which a task is being delegated. The primary purpose of delegation is to decouple code and allow for greater flexibility and reuse.

In programming, and specifically object-oriented programming, this means that when a method is called to do some work, it passes the work on to the method of another object that it has a reference to. The reference could point to whatever object we wish, as long as the object conforms to a predefined set of methods. We call it "programming to an interface" (versus programming to a concrete class implementation). An interface is basically a generic template and has no implementation; it simply means a recipe, a set of methods, preconditions and postconditions (rules).

Now you see I can use whatever implementation I want at any time without changing the code in SomeCaller because the type that doIt() is passed is not concrete but rather abstract since it's an interface. In the Java world, this is often expressed in the service paradigm where you call out to a service (an object advertising itself as a service via a specific interface) and the service then calls out to delegates to help it do its work. The service's methods are named as coarse-grained tasks (makePayment(), createNewUser(), etc.), while internally it does lots if nitty-gritty work through delegation, with the delegates' types being interfaces instead of the concrete implementations.

A delegate object
is an object that another object consults when something happens in that object. For
instance, your repair man is your delegate if something happens to your car. you go to your repair man and ask him to fix the car for you (although some prefer to repair the car themselves, in which case, they are their own delegate for their car).