C # Delegate benefits

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.