C # basics: Delegate,

Source: Internet
Author: User

C # basics: Delegate,

Delegation is the most common content in C. Like classes, enumerations, structures, and interfaces, delegation is also a type. Class is the abstraction of objects, while delegation can be seen as the abstraction of functions. A delegate represents all functions with the same parameter list and return values. For example:

  1. Delegate int GetCalculatedValueDelegate (int x, int y );

In the above definition, we define a delegate, which represents a class of functions. The first parameter of these functions is integer x, and the second parameter is Integer y, the return value of a function is an integer. Here, for the convenience of description, we call this type of function a function with the same signature (Note: This signature is not a concept in the digital signature, but only indicates that such functions have the same parameter list and return values ).

Since a delegate is a type, it can be used to define parameters, variables, and return values. Variables defined by the delegate are used to save function entities with the same signature. Note that, unlike C # And C ++, function pointers in C ++ can only save global or static functions, while delegate entities in C # can refer to any function.

Now let's look at an example. In this example, we use the delegate defined above and create a delegate entity to refer to the AddCalculator function in the program, next, we can use this delegate entity to obtain the computing result just like using the function itself.

  1. Delegate int GetCalculatedValueDelegate (int x, int y );
  2. Static int AddCalculator (int x, int y)
  3. {
  4. Return x + y;
  5. }
  6. Static int SubCalculator (int x, int y)
  7. {
  8. Return x-y;
  9. }
  10. Static void Main (string [] args)
  11. {
  12. GetCalculatedValueDelegate d = AddCalculator;
  13. Console. WriteLine (d (10, 20 ));
  14. }

So far, we can basically understand the meaning of "delegation". For the above Main function, we originally needed to call the AddCalculator function, but it was called through d, that is, the subsequent operations on AddCalculator will be performed by d. James was supposed to go to the teacher's office to get a chalk box. Because james and Xiaowen are good friends, James asked Xiaowen to take it for him, so Xiaowen became Xiao Ming's agent, james commissioned Xiaowen to get the chalk box.

Now let's consider the situation where the delegate is used as a parameter. Using the delegate as a parameter can abstract the processing logic of the function itself, and let the caller decide what logic is used for processing. See the following example:

  1. Delegate int GetCalculatedValueDelegate (int x, int y );
  2. Static int AddCalculator (int x, int y)
  3. {
  4. Return x + y;
  5. }
  6. Static int SubCalculator (int x, int y)
  7. {
  8. Return x-y;
  9. }
  10. Static int Calculator (GetCalculatedValueDelegate del, int x, int y)
  11. {
  12. Return del (x, y );
  13. }
  14. Static void Main (string [] args)
  15. {
  16. Console. WriteLine (Calculator (AddCalculator, 10, 20 ));
  17. }

In the preceding example, the first parameter of the Calculator function is a delegate. In fact, Calculator does not know what to do with x and y. GetCalculatedValueDelegate determines how to deal with x and y. In the Main method, we pass the AddCalculator method as a parameter to Calculator, which means that Calculator can use the logic of AddCalculator to process x and y. This is also very good: Calculator said: "I don't know how to deal with x and y, let del deal with it !" So we threw x and y to del.

This is actually a little similar to the "template method mode. In the template method mode, you can leave the variable part to the subclass for rewriting, while the unchanged part is implemented by the parent class. In this case, when the delegate is used as a parameter, the Calculator can handle the unchanged logic by itself, and delegate the "how to do it" event to others for handling.

Delegation is a common parameter in C. For example, to create a thread, You need to delegate a ThreadStart or ParameterizedThreadStart as a parameter. During thread execution, the function referred to by this parameter is used as the thread execution body. For example, the List <T> type Find method parameter is also a delegate, which leaves the problem of "how to Find" or "How to Find" to developers. Developers only need to define a function whose parameter is T and return a Boolean value, implement the function body, and pass the function as a parameter to the Find method to complete element search in the set.

The return value of a delegate is generally used in the case that different delegates are used according to different situations. This is a bit like the factory mode, but the delegate is often used as the return value without being used as a parameter.

Related Article

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.