C # Delegate an individual's understanding of Delegate
First, you need to know what a delegate is. In terms of plain understanding, you can regard the delegate as a thing used to execute methods (functions.
How to Use Delegation
When using a delegate, you can treat it like a class. That is, declare the object first, and then instantiate the object. The class is called an object or an instance after instantiation, but the delegate is still called a delegate after instantiation.
Statement, such:
1 namespace vczx. procsharp. exc
2 {
3 delegate double mathsop (Double X );
4 // class defination here
5}
This declares a delegate, meaning that any function with a return value of double and only one input parameter of double can be called using this delegate.
Note: The declaration position of the delegate is inside the namespace and outside the class. In fact, the delegate declaration can also be in the class, or even any place where the class can be declared.
Instantiation:
First, we need to first have a method that satisfies the delegate declaration. Assume that a method returns twice the number:
1 class mathsoperations
2 {
3 Public static double multiplyby2 (double value)
4 {
5 return value * 2;
6}
7}
With this method, we can instantiate a delegate:
Mathsop operation = new mathsop (mathsoperations. multiplyby2 );
When instantiating a delegate, you need to give it a parameter. this parameter is the delegate execution method. It can be a static method or an instance method (this is different from the function pointer, function pointers can only call static methods), such:
Mathsop operation = new mathsop (New class1 (). Method1 );
After instantiating a delegate, you can use this delegate to call the method:
Double result = operation (1.23 );
Sample Code:
1 namespace vczx. procsharp. exc
2 {
3 delegate double mathsop (Double X );
4 class start
5 {
6 public class mydelegate
7 {
8 public static double multiplyby2 (Double X)
9 {
10 return x * 2;
11}
12}
13 [stathread]
14 static void main (string [] ARGs)
15 {
16 mathsop operation = new mathsop (mydelegate. multiplyby2 );
17 Double X = 1.23;
18 double result = operation (X );
19 console. writeline ("{0} multiply by 2 is {1}", X, result );
20 console. Read ();
21}
22}
23}
Multi-channel broadcast Delegation
The delegate used previously only contains one method call. The number of calls to the delegate is the same as the number of calls to the method. If you want to call multiple methods, you need to call the delegate multiple times. In fact, the delegate can also contain multiple methods. This delegate is a multi-channel broadcast delegate. Multi-channel broadcast delegate is derived from system. multicastdelegate. Its combine method allows multiple call methods to be linked together. We can add a call Method to the delegate through + =, or use-= to delete the call method. For example:
1 namespace vczx. procsharp. exc
2 {
3 Public class mydelegate
4 {
5 public static void multiplyby2 (double value)
6 {
7 double result = value * 2;
8 console. writeline ("multiplying by 2: {0} gives {1}", value, result );
9}
10
11 public static void squre (double value)
12 {
13 double result = value * value;
14 console. writeline ("squaring: {0} gives {1}", value, result );
15}
16}
17
18 delegate void mathsop (Double X );
19
20 class start
21 {
22 [stathread]
23 static void main (string [] ARGs)
24 {
25 mathsop operation = new mathsop (mydelegate. multiplyby2 );
26 operation + = new mathsop (mydelegate. squre );
27 double * = 1.23;
28 operation (X );
29
30 operation-= new mathsop (mydelegate. multiplyby2 );
31 operation (X );
32
33 console. Read ();
34}
35}
36}
Output:
Multiplying by 2: 1.23 gives 2.46
Squaring: 1.23 gives 1.5129.
Squaring: 1.23 gives 1.5129.
Note: The void must be returned when the multi-channel broadcast delegate declaration is made. Otherwise, the return value does not know where to send it back. In this regard, I did a test: If the void is not returned for the delegate declaration, the return value is the return value of the method of the last link into the delegate chain, and compilation will not go wrong.
Why use delegate
Delegate allows programmers to encapsulate method references in the delegate object. The delegate object can then be passed to the code that can call the referenced method, without knowing which method will be called during compilation. Unlike function pointers in C or C ++, delegation is object-oriented and type-safe.