Purpose of Delegates in C# (C Sharp)In your application, how do you handle events? Or Do you have to use a method wherein the method name can be determined only at run time? Or at any situation, did you prefer having functional pointer concept of C++ in C#? If so, then you can use Delegates to achieve it.
Delegates
are used to wrap a method. You can invoke that method by calling the delegate.
In other words, delegates are methods that accept another method as parameter.
This method might be determined at run time only. Not just one method,
they can be used to invoke set of methods in a pre-defined order. In simple
words, delegates are reference types used to encapsulate methods. This
concept is very similar to function pointers in C++ but still there is
a major difference between both. Delegates are secure enough and they
are object-oriented. But function pointers in C++ lack these two important
features. For
handling events Given below is a simple example for delegates: public delegate
void sampleDelegate(int param1); In this example, a delegate called sampleDelegate is declared. Class testDelegate has a static method testFunc1 and an instance method testFunc2. Both these methods have the same signature as that of sampleDelegate. Class useDelegate has Main( ) method that uses sampleDelegate to wrap methods defined in testDelegate class. Inside Main( ) method, an instance of sampleDelegate called delegate1 is wrapping the static method testFunc1 of testDelegate class. Since it is a static method, the method is accessed using the class name. A value is now passed to delegate1 using the code delegate1(10);
. This is what delegation is all about. Method invocation of testFunc1 is delegated to delegate1 and the parameter value is passed to delegate1 instead of testFunc1. Another instance of the delegate called delegate2 is used to wrap the instance method testFunc2. Since it is an instance method, an instance of testDelegate called testObj is created and testFunc2 is accessed using testObj instead of class name. Other than static and instance methods, delegate can wrap another delegate also. That is shown using delegate3. Note that sampleDelegate cannot be used for wrapping testFunc3 of testDelegate. This is because testFunc3 has a different signature when compared to the signature of sampleDelegate. In this example three instances of sampleDelegate is used. Each instance is used for calling a different method. Instead of creating individual instances, delegates offer a much more advanced feature known as multicasting. With multicasting, you can create a single instance of sampleDelegate and invoke all the methods matching its signature. Class useDelegate in the above example is further modified as: class useDelegate
{ In this example, same delegate object is used to execute all methods. Output of this code will be: The value
passed to static method testFunc1 is:10 Are you confused by looking at the output? Heres the justification for the order of execution. Inside Main( ) method an instance of sampleDelegate called delegateObj is created and initialized to null. Then it wraps testDelegate.testFunc1. delegateObj is then invoked with parameter value as 10. This parameter will now be passed to testDelegate.testFunc1 and the print statement inside the function is executed. To access the instance method of testDelegate class, an instance of the class called testObj is created and the delegateObj wraps the instance method of testDelegate using testObj. But keenly notice that specific line of code. Repeating it here for your reference: delegateObj += new sampleDelegate(testObj.testFunc2); See the operator +=. This operator appends testObj.testFunc2 inside the delegate along with its earlier method testDelegate.testFunc1. Hence on invoking delegateObj using the statement delegateObj(20), all the methods that are there in the invocation list(in this example testDelegate.testFunc1, testObj.testFunc2) will be executed. Order of execution is same as the order in which methods are appended into the delegate. In the next line of code, yet another operator is used: delegateObj -= new sampleDelegate(testDelegate.testFunc1); -= operator is used to remove a method from the invocation list of delegate. As per this example, testDelegate.testFunc1 is removed. Therefore while invoking the delegate, only the method testObj.testFunc2 will be executed. While performing multicasting, ensure that the return type of the delegate is void. If not, then while invoking the delegate only the return value of the recently added method will be displayed. Return value of all other methods will be lost.
|