C # simple understanding of events 1,

Source: Internet
Author: User

C # simple understanding of events 1,

EventArgsClass that contains event dataBase class,And provideDoes not containThe value of the event in the event data.

When the event to be defined contains event data, you can derive the Custom Event Data class from the EventArgs option class. The code and result are as follows:

Public class InviteEventArgs: EventArgs // custom event class {public string Message; public InviteEventArgs (string msg) // constructor, input parameter {Message = msg ;}} public class Yiqiok // event publisher {public delegate void InviteHandler (object sender, InviteEventArgs e); // custom delegate type public event InviteHandler LolInvite; // use the custom delegate type to define the event public void InviteComing (string msg) // issue the event {if (LolInvite! = Null) // check whether an event processing method is added {// Console. writeLine (msg); LolInvite (this, new InviteEventArgs (msg); // trigger event }}public class Classmate // event subscriber {private string name; public Classmate (string Name) {name = Name;} public void SendResponse (object s, InviteEventArgs e) // event processing function, which must match the custom delegate type {Console. writeLine (e. message); Console. writeLine ("from:" + this. name + "reply: you have received the invitation and can start at any time! ") ;}} Public class Start {static void Main () {Yiqiok yiqiok = new Yiqiok (); // initialize Classmate classmate1 = new Classmate (" Lna "); classmate classmate2 = new Classmate ("Jim"); Classmate classmate3 = new Classmate ("Cry"); Classmate classmate4 = new Classmate ("Tom"); yiqiok. lolInvite + = new Yiqiok. inviteHandler (classmate1.SendResponse); // subscribe to the event yiqiok. lolInvite + = new Yiqiok. inviteHandler (classmate2.Sen DResponse); yiqiok. lolInvite + = new Yiqiok. inviteHandler (classmate3.SendResponse); yiqiok. lolInvite + = new Yiqiok. inviteHandler (classmate4.SendResponse); yiqiok. inviteComing ("yiqiok ??? "); // Send a notification }}

Result:

For more details, refer to MSDN

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.