Why use events? For example, you have a bottom button on your interface, and you have written a message window code by clicking this button, however, if you want to bring up the message window, the developer does not know it, so the developer has defined this event in advance and does not care what you will do when you click this button, but the button calls your code by clicking the event.
In fact, the event is simple: Just like a delegate, it only has an extra constraint. This constraint is triggered when XXX reaches a certain time point (a certain point;
I still don't understand it. It doesn't matter. Let's take a look at a small example. You usually like to pay attention to Weibo. Let's take this as an example. It may not be very good. Let's take a look;
Fans (fans) Pay attention to Big V (bigv), which is equivalent to subscribing to an event.
Big V posted a microblog, which is equivalent to an event.
In this event, fans received the Weibo account of big v. the following code is specific:
This is the content of Weibo, that is, a parameter of the event.
public class MicroBlogEventArgs : EventArgs { private DateTime sendDateTime; public DateTime SendDateTime { get { return this.sendDateTime; } } private string author; public string Author { get { return this.author; } } private string context; public string Context { get { return this.context; } } public MicroBlogEventArgs(DateTime createDateTime, string author, string context) { this.sendDateTime = createDateTime; this.author = author; this.context = context; } }
This is a large v object, and a large V defines an event.
Public class bigv {private string name; Public bigv (string name) {This. name = Name;} public event eventhandler <microblogeventargs> sendmicroblog; // <summary> // an event is generated by Big V (that is, a microblog is sent by Big V) /// </Summary> /// <Param name = "E"> </param> Public void onsendmicroblog (microblogeventargs e) {system. threading. interlocked. compareexchange (ref sendmicroblog, null, null); If (sendmicroblog! = NULL) {sendmicroblog (this, e) ;}} public void simulatesendmicroblog (datetime DT, string context) {microblogeventargs E = new microblogeventargs (DT, this. name, context); onsendmicroblog (e );}}
Below is the fans. As long as fans follow (subscribe) The Big V, they can see the big V Weibo.
Public class fans {public fans (bigv) {bigv. sendmicroblog + = new eventhandler <microblogeventargs> (bigv_sendmicroblog);} void bigv_sendmicroblog (Object sender, microblogeventargs e) {console. writeline ("{0}, {1} sent: {2}", E. senddatetime, E. author, E. context );}}
The following is a simulation. Big V posts Weibo, and then followers of his fans will be able to receive his Weibo.
class Program{ static void Main(string[] args) { BigV bigV = new BigV("zhangsan"); Fans fans = new Fans(bigV); bigV.SimulateSendMicroBlog(DateTime.Now, "NO ZUO NO DIE"); Console.ReadKey(); }}
In fact, we all know what an event is, but we don't feel like we can even write it out when we use code. So you may wish to find a few more examples on the Internet to practice it. Let's do it today.