Delegate event, c # Delegate event
Class PubEventArgs: EventArgs {private readonly string _ magname; public string Magname {get {return _ magname ;}} public PubEventArgs (string magname) {_ magname = magname ;}} class Event // create an event class {public delegate void PubComputerEventHandle (object sender, PubEventArgs e); // declare to delegate public Event PubComputerEventHandle PubComputer; // create a delegate chain, that is, the event public delegate void PubLifeEventHandle (object sender, PubE VentArgs e); public event PubLifeEventHandle PubLife; public virtual void OnComputer (PubEventArgs e) {PubComputerEventHandle handle = PubComputer; if (handle! = Null) {handle (this, e) ;}} public virtual void OnLife (PubEventArgs e) {PubLifeEventHandle handle = PubLife; if (handle! = Null) {handle (this, e) ;}} public void IsComputer (string magname) // create a trigger event process {Console. writeLine ("release" + magname + "magazine"); OnComputer (new PubEventArgs (magname);} public void IsLife (string magname) {Console. writeLine ("release" + magname + "magazine"); OnComputer (new PubEventArgs (magname) ;}} class Man // create operation subject {public string name; public Man (string name) {this. name = name;} public void Order (object sender, PubEventArgs e) // create the delegate function {Console. writeLine (name + "subscription" + e. magname + "publication") ;}} class Program {static void Main (string [] args) {Event e = new Event (); // create Man zs = new Man ("Zhang San") for the event; // create e for the subject. pubComputer + = new Event. pubComputerEventHandle (zs. order); // call the principal's entrusted function Man ls = new Man ("Li Si"); e. pubComputer + = new Event. pubComputerEventHandle (ls. order); e. pubLife + = new Event. pubLifeEventHandle (ls. order); e. isComputer ("computer"); // trigger event e. isLife ("life"); Console. read ();}}