Subscription and publication of C # event events

Source: Internet
Author: User

Tag: Stat code col implements event pass www. Color Registration

Let's use a simple example to illustrate the mechanism of this message passing.

There is a family of three, the mother is responsible for cooking, father and children eat ... Think of these three people as three classes.

Mother has a method called "cooking". There is an event called "Dinner". After the meal, call the development event and post the dinner message.

Father and child have a method, called "Eat".

The father and the children's "eat" method, registered to the mother's "Dinner" event. That is, subscribe to Mom's dinner message. Let mom finish dinner, when the news, tell Dad and children.

This mechanism is in C #, subscription publishing. Here's what we do with code:

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 6 namespaceEventsimpledemo7 {8     class Program9     {Ten         Static voidMain (string[] args) One         { AMom mom =NewMom (); -Dad dad =NewDad (); -Son son =NewSon (); theMom. Eat + =NewAction (dad. Eat);//Subscribe -             //mom. Eat + = new Action (son. Eat); -  - mom. Cooking (); + Console.readkey (); -         } +     } A  at      Public classMom -     { -         //Public delegate void Delegateaction (); -          Public EventAction Eat;//Action can be changed to delegate method: Delegateaction -  -          Public voidCooking () in         { -Console.WriteLine ("Mom: Dinner's ready! "); to             if(Eat! =NULL) +             { - Eat (); the             } *         }  $     }Panax Notoginseng  -      Public classDad the     { +          Public voidEat () A         { theConsole.WriteLine ("dad: Come right now! "); +         } -     } $  $      Public classSon -     { -          Public voidEat () the         { -Console.WriteLine ("son: I'll eat it later! ");Wuyi         } the     } -  Wu}

Source: http://www.cnblogs.com/David-Huang/p/5150671.html

The author's story is easy to understand, and can't help copying it.

Subscription and publication of C # event events

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.