Events in C #-subscriptions and publications

Source: Internet
Author: User

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 class Program2     {3          Public Static voidMain (string[] args)4         {5             //instantiating an object6Mom mom =NewMom ();7Dad dad =NewDad ();8Child child =NewChild ();9             Ten             //register your father and child's Eat method with your mother's Eat event One             //Subscribe to the news of Mom's dinner AMom. Eat + =Dad. Eat; -Mom. Eat + =Child . Eat; -              the             //call Mom's Cook event - mom. Cook (); -              -Console.Write ("Press any key to continue ..."); +Console.readkey (true); -         } +     } A      at      Public classMom -     { -         //define eat events for post meal messages -          Public EventAction Eat; -          -          Public voidCook () in         { -Console.WriteLine ("Mom: Dinner's ready."); to             //Dinner 's ready, post dinner message + Eat (); -         } the     } *      $      Public classDadPanax Notoginseng     { -          Public voidEat () the         { +             //dad, go eat. AConsole.WriteLine ("dad: it's dinner. "); the         } +     } -      $      Public class Child $     { -          Public voidEat () -         { the             //bear Children lol, after the fight to eat -Console.WriteLine ("Child: Finish the game and eat. ");Wuyi         } the}

Operation Result:

  What do you do when Grandpa and Grandma come to the guest? And the father and the child, write a eat method, the same registration to the mother's dinner event is good.

Events in C #-subscriptions and publications

Related Article

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.