Delegation and events

Source: Internet
Author: User

I haven't been on this land for some days. I'm sorry ..

The word "delegate" should not be a stranger to us. But I am a little depressed when this word appears in the computer. What is the subtle relationship between the two?

First, delegation is a type that is abstracted from methods. It is a type-safe class that defines the type of the return type and parameter. When we use a class, there are two stages. First, we need to define this class to tell the compiler what fields and Methods This class consists of) and then an object of the instantiated class. The same applies to delegation. The definition of a delegate is similar to the definition of a method, but there is no method body. Its life is as follows:

Delegate void AppleHanlder (int x); in this example, a delegate name is defined as AppleHanlder, and each instance of the delegate can contain a reference to a method. This method has an int parameter, the return type is void.

So when do I use delegation? I often see numbers passed as parameters. How can I pass methods as parameters? It sounds a bit strange, because we are not used to this practice. In fact, we need to use delegation at this time. A delegate can pass a method and call multiple methods at the same time, which is much easier than a separate method call.

Based on Delegation, events provide a publishing/subscription mechanism for delegation.

Next we will use a simple code to understand the relationship between the two.

The three classes without any relationship can be associated with one delegate. The following simple program can be seen as a simple factory.

Namespace prjtest11 {// declare a delegate. The return type of the abstract method is void, and the parameter list is empty. delegate void CryHallder (); /// <summary> // The "cry" event in the Baby class is the event source, once this event is triggered, it will immediately call the method for subscribing to this event. All subscribed events will be called.) // </summary> class Baby {// creates a delegate event, use event to modify public event CryHallder cryEvent; public void OnCry () {Console. writeLine ("I cried .... "); // Determines if the event is empty. if the event is not empty, the if (cryEvent! = Null) {// trigger event cryEvent () ;}}} class Father {public void Beat () {Console. WriteLine ("do not cry, then cry again... ") ;}} Class Mather {public void Feed () {Console. WriteLine (" Don't cry, just give it to you... ") ;}} Class Program {static void Main (string [] args) {Baby B = new Baby (); Father f = new Father (); mather m = new Mather (); // The Beat method in the Father class subscribes to the crying event B in the Baby class. cryEvent + = new CryHallder (f. beat); // The Fead method in the Mather class subscribes to the crying event B in the Baby class. cryEvent + = new CryHallder (m. feed); // Let the crying event happen B. onCry () ;}} class does not have any relationship with the three mines

This article is from the "Ajax girl" blog!

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.