I am confused that what is the actual role of a delegate?
I have been asked this question many times in my interviews, but I don't think that interviewers were satisfied with my answer.
Can anyone tell me the best definition, in one sentence, with a practical example?
Delegates is mainly used with events.
The need is:
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 :
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 simply a function pointer.
simply put you assign the method you wish to run your delegate. then later in code you can call that method via Invoke.
some code to demonstrate (wrote this from memory so syntax may be off)
Think about delegate as about a simplified implementation of Command pattern.
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.
Here's a reference for C# you can look at:
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:
We could declare a delegate signature like this:
And then we could declare a method which takes the delegate as a parameter like this:
And we could call the
CalcMyTotal
method passing in the delegate method we wanted to use.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
public delegate type_of_delegate delegate_name() // Declaration
The following example shows a delegate operation:
What is Multicast Delegate?
It is a delegate which holds the reference of more than one method. Multicast delegates must contain only methods that return void, else there is a run-time exception.
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.
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.
Simple Delegate
http://knowpacific.wordpress.com/2012/01/26/delegate/