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