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