What's a delegate
A delegate is a type that safely wraps/encapsulate a method. It is similar to 'function pointers' in C/C++, but it's type safe.
When to use a delegate
Use a delegate when :
- An eventing design pattern is used.
- It is desirable to encapsulate a static method.
- The caller has no need access other properties, methods, or interfaces on the object implementing the method.
- Easy composition is desired.
- A class may need more than one implementation of the method.
There was a good example given on a book that I recently referenced.
'Consider your will—your last will and testament. It’s a set of instructions—“pay the bills, make a donation to charity, leave the rest of my estate to the cat,” for instance. You write it before your death, and leave it in an appropriately safe place. After your death, your attorney will (you hope!) act on those instructions.'
In order to a delegate to proceed, following four things need to happen:
* The delegate type needs to be declared.
* There must be a method containing the code to execute.
* A delegate instance must be created.
* The delegate instance must be invoked.
Declaring delegate type
Declaration of a delegate type specifies what kind of action can be represented by instances of the type.
If we are creating an instance of the above delegate, we should have a method with one string parameter and void return type.
Method containing the code to execute
There should be a method which matches the signature of the delegate. Method can be either static or non static. I will take two examples to illustrate a static method and a non static method. I will use the following two methods in the 'DelegateClass' class.
Creating a delegate instance
When creating delegate instances static methods can be passed directly. But for non static methods an instance should be created and passed that to the delegate.
E.g : [Static Method]
E.g : [Non Static Method]
Invoking delegate method
It's a matter of calling the Invoke method on the delegate instance.
Here is the complete source code for the above example. I have used a console application to illustrate this :
And when you run your application you will get the following output:
Instead of using separate delegate instances, you can combine and execute delegates. We have to use the 'Combine' method in Delegate class. To illustrate this I will alter my 'DelegateClass' as shown below :
To create instances and invoke :
The complete source code :
An when you run the application, you will get the following output: