I 've learned about delegation over the past few days. It's a headache, and my head is not enough. After several twists and turns, I finally seem to have some understanding about delegation. The following is a summary:
What is delegation?
A delegate is to define a reference to a method, similar to a function pointer in c ++.
Delegate format?
Modifier 1 delegate modifier 2 delegate type name (Variable n );
Modifier 1 is the range modifier of the defined delegate. It is commonly used as public;
Modifier 2 is the type modifier of the return value, such as void, string, and char;
The name is obtained when we define a delegate type;
We often have doubts about Variable n. Is delegation a method? Why are there return values and variables?
In fact, a delegate is not a method, but a type. The reason why it has returned values and variables is to keep them consistent with the delegate method;
Note: A delegate is a type, so we often define it outside a class;
Also, after the definition of the delegate type, we cannot directly use it. We also need to define the delegate variable of this type for it;
Format:
Modifier 1 delegate modifier 2 delegate type name (Variable n );
Delegate type name delegate variable;
During use, you also need to note that we need to use the defined delegate variable instead of the defined delegate type name, which is similar to the enumerated type we define; because the defined delegate variable is a variable, We need to assign values to it before use.
Format: modifier 1 delegate modifier 2 delegate type name (Variable n );
Delegate type name delegate variable;
Delegate variable = delegate legal name;
Note: The delegate method name here is the name of the method you want to delegate. Note that you should not add the parameters and brackets of the method. At the same time, the delegate method must be consistent with the return value type, parameter list type, and number of parameters defined during the delegate type definition;
OK. Now the delegate type has been defined. Do you still remember what the delegate is?
Yes, delegates are used to pass methods as parameters into other methods,
Therefore:
We need to define a method first;
Range modifier [static] Return Value Type modifier call method name (Variable n, delegate type delegate variable)
{
Delegate variable (Variable n );
.
.
.
Delegate variable (Variable n );
}
The above Variable n is the parameter of the method you delegate;
Started to use:
1) delegate type delegate variable;
2) delegate variable = delegate legal name;
3) Call method name (Variable n, delegate variable );
Complete example:
Using System;
Using System. Collections. Generic; using System. Text;
Example of namespace Delegation
{
Public delegate void del (string str); // define the delegate type
Calss example
{
Static void send (string I) // method to delegate
{
Console. WriteLine (I );
}
Static void sendup (string list, del handler) // CALL THE METHOD
{
Handler (list );
}
Public static void mian (string [] args) // master Method
{
Del delegate variable = send; // the sentence is equivalent to del delegate variable; delegate variable = send;
Sendup ("this is an example of delegation !!! ", Delegate variable );
Console. ReadKey ();
}
}
}