C # simple understanding of events,
Events are an important feature of C. Events mainly involve publishers, subscribers, and event handlers.
Using the pre-defined delegate type in the. net class library, you can easily define events. After the publisher triggers an event, the subscriber executes the event processing function. The code and running result are as follows:
Public class Yiqiok // event publisher {public event EventHandler LolInvite; // use. NET class library pre-defined delegate type definition event public void InviteComing (string msg) // issue event {if (LolInvite! = Null) // check whether an event handling method is added {Console. writeLine (msg); LolInvite (this, new EventArgs (); // trigger event }}public class Classmate // event subscriber {private string name; public Classmate (string Name) {name = Name;} public void SendResponse (object s, EventArgs e) // event processing function, which must match the predefined delegate type {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 EventHandler (classmate1.SendResponse); // subscribe to the event yiqiok. lolInvite + = new EventHandler (classmate2.SendResponse); yiqi OK. LolInvite + = new EventHandler (listener); yiqiok. LolInvite + = new EventHandler (listener); yiqiok. InviteComing ("yiqiok ??? "); // Send a notification }}