C # Study Notes Series II: Delegate

Source: Internet
Author: User

The. NET language has one more delegate than other object-oriented languages (I'm not sure if Java has this concept, as though I have never seen it ). It's hard to understand, so sort out your ideas.

In general, delegation is a way to convert a function into a variable, so that the function can be assigned a value or superimposed. In PHP, the language itself uses a simple feature to support similar basic methods. If any string variable is followed by parentheses, it can be executed as a function, the system automatically finds the function with the same variable value for execution.

In C #, delegates greatly enhance this application. You can declare a delegate (a common function declaration, modifier plus delegate, and only a function declaration including parameters without function bodies ), then, use the delegate name to declare a specific variable. Now, you can add a function body that is the same as the delegate declaration method. In addition, multiple functions can be added to the same delegate variable. Both static and Instance functions can be used.

When the delegate variable is called by running a function at a certain position, all functions bound to it are executed sequentially. This is the implementation method in which VB. NET and C # bind their functions (methods) to an existing class event (also a function) and are actively called by the other party. In fact, the other class calls its own delegate variable in the middle of a method.

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.