. Net delegate (delegate type)

Source: Internet
Author: User

Essentially, a delegate is a type-safe object pointing to several methods (similar to a C ++ function pointer ). The maintenance of a delegate has three important information: 1. Address 2 of the method to be called, parameter 3 of the method (if any), and return value of the method (if any ). Unlike C ++ function pointers,. NetThe delegate can point to static methods and instance methods.

 

First, let's take a look at how to define a delegate (using keywordsDelegate)

  1. // This delegate can point to any method that receives two int parameters and returns the int value.
  2. // Binary operation
  3. Public Delegate int binaryop (int x, int y );

When you declare this binaryop delegate, the compiler will generate such a class for you:

  1. Sealed class binaryop: system. multicastdelegate
  2. {
  3. Public binaryop (object target, uint functionaddress );
  4. Public int invoke (int x, int y );
  5. Public iasyncresult begininvoke (int x, int y,
  6. Asynccallback CB, object State );
  7. Public int endinvoke (iasyncresult result );
  8. }

Among them, invoke is the core method. As the name suggests, it is used to call every method maintained in the delegate. Begininvoke () and endinvoke () are used to call the current method asynchronously. This requires that you have a multi-threaded programming background, which is beyond the scope described in this article. Here, you only need to understand that the delegate actually secretly implements the function that would have to be implemented using multiple threads.

To be continued ......

I may feel a bit confused when I first recognize the delegation. Now let's take a complete example:

  1. Namespace simpledelegate
  2. {
  3. // This delegate can point to any method that receives two int parameters and returns the int value.
  4. // Binary operation
  5. Public Delegate int binaryop (int x, int y );
  6. // This class contains the method to be pointed to by binaryop
  7. //
  8. Public class simplemath
  9. {
  10. Public static int add (int x, int y)
  11. {Return X + Y ;}
  12. Public static int subtract (int x, int y)
  13. {Return x-y ;}
  14. }
  15. Class Program
  16. {
  17. Static void main (string [] ARGs)
  18. {
  19. Console. writeline ("****** simple delegate example ****** \ n ");
  20. // Create a binaryop object pointing to simplemath. Add ()
  21. //
  22. Binaryop B = new binaryop (simplemath. Add );
  23. // Call the add () method and indirectly use the delegate object.
  24. Console. writeline ("10 + 10 is {0}", B (10, 10 ));
  25. Console. Readline ();
  26. }
  27. }
  28. }

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.