A delegate is a type that defines a function signature, so that an instance of a delegate can call methods that match it. In C++ terms, a delegate is a fancy way to describe a function pointer.
A delegate declaration consists of four parts:
The keyword delegate
Followed by the return type of the function to be called
Followed by a name that will be generic to the function to be called
Followed by a parameter list
In the following example, the delegate called MyDelegate can call any function that returns an int and is passed a single string:
delegate int MyDelegate(string s);
You can use delegates to fire a single method, or you may multicast methods. You may initialize a new delegate using static or instance methods, using the following syntax:
MyDelegate Del1 = new MyDelegate(DoSomething);
You can also initialize a delegate using this syntax for multicasting:
MyDelegate Multicast = null; m += new MyDelegate(DoSomething); m += new MyDelegate(DoSomething2);
After you have initialized your delegate with values, you use it by calling the delegate, as follows:
del1(MyString);
In the multicast case, you call the delegate as follows:
m(MyString);
This will result in the DoSomething method being called, followed by the DoSomething2 method.
To remove a method from the delegate list, use this syntax:
m -= new MyDelegate(DoSomething2);
This will remove DoSomething2 from the delegate known as m.
| Note?/td> |
All delegates support asynchronous and synchronous calls of their invocation list via the methods BeginInvoke, EndInvoke, and Invoke. |
You can also iterate through a list of all the delegates via the following code:
foreach(Delegate D in MyInstanceDelegate(GetInvocationList())
{
(cast)D(params);
}
It is also important to realize that every time you declare a delegate, a new class is generated. This class does the work for you under the hood.
You should consider using a delegate in the following situations:
You want to use a C-style function pointer.
You desire a single callback invocation.
You want the callback function registered at construction time rather than via a method call.
You should use an interface if the callback function involves complex behavior or when using Remoting.
The following example demonstrates using delegates.
using System; namespace Client.Chapter_8___Delegates_Events_and_Namespaces { class Delegates { //Creates a method pointer delegate int MyDelegate(string s); static void Main(string[] args) { MyDelegate Del1 = new MyDelegate(DoSomething); MyDelegate Del2 = new MyDelegate(DoSomething2); string MyString = "Hello World"; Del1(MyString); Del2(MyString); //Or you can multicast delegates by doing this MyDelegate Multicast = null; Multicast += new MyDelegate(DoSomething); Multicast += new MyDelegate(DoSomething2); //Both DoSomething & DoSomething2 will be fired //in the order they are added to the delegate Multicast(MyString); Multicast -= new MyDelegate(DoSomething2); } static int DoSomething(string s) { return 0; } static int DoSomething2(string s) { return 0; } } }