Delegate event, c # Delegate event

Source: Internet
Author: User

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 ();}}

 

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.