C #: Wen Zhixin-delegation and events (Delegation)

Source: Internet
Author: User

Introduction: delegation is a type. Its position is the same as that of class, struct, and enum. We can say this: A delegate can be declared in any place where the class can be declared. We often put the delegate and event together for understanding. In fact, this is only an application of the delegate. To understand the nature of delegation, we must investigate the nature of the phenomenon. What is the purpose of delegation?

Symptom:

Public Delegate void mydelegate (); Public Delegate string mydelegate (string name );

From the above two delegate statements, we will find that the delegate seems very similar to the method statement. Indeed, the delegate has a return type and a list of corresponding parameters, which are actually the "return type" and "parameter list" that limit addition (or "Registration"). to the method type in the delegate, that is, only the return type and parameter list (parameter type, number, etc.) that are the same as the delegate can be registered or added in the delegate.

Essence: the essence of delegation is to encapsulate the methods, but there are rules for the methods encapsulated into the delegation. From the perspective of the memory stack, the delegate encapsulation is not a specific method, but the address of the method in the memory heap (a bit like the function pointer in C ++, in fact, I personally understand this ). In addition, the methods added to the delegate are sequential. WhenProgramWhen a delegate is called, it calls the method according to the sequence in the method list. If the return type is specified during the delegate definition, and there are multiple methods in the delegate, after the delegate is called, the returned value is the return value of the last method. The returned values of the previous method are ignored by the program.

Learn new things with a gentle understanding-looking back at the knowledge you 've learned before, maybe new discoveries and gains.

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.