Let's look at an example:
A person has three sons, asking them to take one thing out and bring back one prey.
It can be understood as a father's commitment to his son:
Prey method (Tool)
The methods for entrusting the three members are different.
Rabbit hunting (Tool bow)
Buy pheasant (Tool money)
Wolf Trap (Tool trap)
What is delegation?
A delegate is a type used to indicate all methods in the same form (the return values are of the same type and parameters are the same ).
Public Delegate double handler (double [] DS );
Public double sum (double [] DS) {// perform the operation in the method .}
Public double average (double [] DS) {// perform the operation in the method .}
Delegation instantiation
Use the New Keyword (using the method name as a parameter) to generate a delegate object and establish the association between the delegate and the method.
Example:
Handler handler = new handler (SUM );
Use Delegation
You can use a delegated instance like a variable. When using a delegate, you must pass parameters to the delegate as required.
If the delegate is used, the method associated with the delegate is called.
Example:
Double [] weights = {1.0, 2.0, 3.0, 4.0 };
Double result = handler (weights );
A single delegate can be associated with multiple methods through a + operator delegate, which is called a multi-channel broadcast delegate (corresponding to a single-channel broadcast delegate ).
If a delegate is used at this time, all associated methods are called.
Example:
Handler handler = new handler (SUM );
Handler + = new handler (average );
To put it bluntly, the delegate is a third party. The caller tells the third party what to do, and then the caller does not have to worry about it.
This delegate (a third party) will call the method to help you implement it.
Benefits of delegation:
1. It is equivalent to using the method as another method parameter (similar to the function pointer of C)
2. serve as a bridge between two methods that cannot be called directly. For example, delegate is required for cross-thread method calls in multiple threads.
3. Use delegation when you do not know the specific implementation of the method. For example, use delegation in an event.
Let's look at an example:
A person has three sons, asking them to take one thing out and bring back one prey.
It can be understood as a father's commitment to his son:
Prey method (Tool)
The methods for entrusting the three members are different.
Rabbit hunting (Tool bow)
Buy pheasant (Tool money)
Wolf Trap (Tool trap)
What is delegation?
A delegate is a type used to indicate all methods in the same form (the return values are of the same type and parameters are the same ).
Public Delegate double handler (double [] DS );
Public double sum (double [] DS) {// perform the operation in the method .}
Public double average (double [] DS) {// perform the operation in the method .}
Delegation instantiation
Use the New Keyword (using the method name as a parameter) to generate a delegate object and establish the association between the delegate and the method.
Example:
Handler handler = new handler (SUM );
Use Delegation
You can use a delegated instance like a variable. When using a delegate, you must pass parameters to the delegate as required.
If the delegate is used, the method associated with the delegate is called.
Example:
Double [] weights = {1.0, 2.0, 3.0, 4.0 };
Double result = handler (weights );
A single delegate can be associated with multiple methods through a + operator delegate, which is called a multi-channel broadcast delegate (corresponding to a single-channel broadcast delegate ).
If a delegate is used at this time, all associated methods are called.
Example:
Handler handler = new handler (SUM );
Handler + = new handler (average );
To put it bluntly, the delegate is a third party. The caller tells the third party what to do, and then the caller does not have to worry about it.
This delegate (a third party) will call the method to help you implement it.
Benefits of delegation:
1. It is equivalent to using the method as another method parameter (similar to the function pointer of C)
2. serve as a bridge between two methods that cannot be called directly. For example, delegate is required for cross-thread method calls in multiple threads.
3. Use delegation when you do not know the specific implementation of the method. For example, use delegation in an event.