[C # advanced series] 10 events,

Source: Internet
Author: User

[C # advanced series] 10 events,

Event, which defines the type of event members that allow instances of the type or type to notify other objects of specific events.

According to my own understanding, an event can be noticed by (method) or canceled by (method). After an event occurs, the party concerned with the event will understand that, and respond accordingly to the event (execution method ). (I understand it this way every time, so that I can understand it more literally)

As we all know, events are actually based on delegation. Delegation is a type-safe method for calling callback functions.

Today, I wrote a Demo about the breakup event, which is vivid.

Define the event parameter class (skip this step)

If you want to transmit additional parameter information after an event occurs, you need to define the event parameter class and inherit EventArgs. Otherwise, you can directly use EventArgs. Empty. (EventArgs. Empty is actually new EventArgs ())

Public class break up EventArgs: EventArgs {public break up EventArgs (string name, string title) {this. _ The person who broke up = name; this. _ reason for breaking up = title;} public string: {get {return _ reason for breaking up;} public string reason for breaking up {get {return _ reason for breaking up ;}} private readonly string _ the person who broke up; private readonly string _ the reason for breaking up ;}

Define event members

Public class Troy {// The Declaration of the delegate type EventHandler <T> public delegate void EventHandler <TEventArgs> (object sender, TEventArgs e); // The first object is the event sender as the name suggests, here it must have been Troy. The event // The second parameter is passed. The additional event information we previously defined is public event EventHandler. <break up EventArgs> claim to break up ;}

Now there is a Troy claim to break up.

Now that the event exists, let me trigger the event.

Events

So it becomes like this.

Public class Troy {// The Declaration of the delegate type EventHandler <T> public delegate void EventHandler <TEventArgs> (object sender, TEventArgs e); // The first object is the event sender as the name suggests, here it must have been Troy. The event // The second parameter is passed. The additional event information we previously defined is public event EventHandler <break up EventArgs>; // define the method for triggering the event to notify the person who has followed the event. Generally, it must be defined as protected and virtual protected virtual void On claiming to break up (break up EventArgs e) {// if someone listens to me about this, it means that no one listens to me, and you can't talk to yourself in the program, but it seems silly (perfect and good image) if (claim to break up! = Null) this. Claim to break up (this, e );}}

This book also introduces the writing of events caused by thread security considerations,

Because before I claim that I want to break up, the person who was supposed to listen to me, A, has another person, and B, calls him, A He suddenly ran away (the delegate chain claimed to be broken up in another thread was removed from the delegate ), at this time, no one heard about the claim to break up (claim to be null), and I reported a Null exception if I couldn't spit it out for a long time.

So with the following method

Protected virtual void On claims to break up (break up EventArgs e) {// the following code means: I want to say that when I break up, I will focus on the people who claimed to be breaking up this event to the discussion group. // then, even if the other thread is called by anyone next to me, in fact, I dropped them to the discussion group. // at this time, there are Members in the discussion group. The followers still learned the sad story: var discussion group = System. threading. volatile. read (ref claim to break up); // Volatile. read only serves as a reference to the discussion group that declares that it is about to break up. The reason why Read does not need to be equal is that it may be removed from the temporary variable discussion group during Compiler optimization. If (discussion group! = Null) this. Claim to break up (this, e );}

However, the JIT compiler indicates that, in fact, such use can be equal, just to prevent them from happening.

However, Here I just want to break up quietly, so let's follow the above statement.

Real events

I can say many breaking up events every day, some are you, some are you.

In order to make it more accurate, I may want to send a message, that is, this time I may break up.

Public class Troy {// The Declaration of the delegate type EventHandler <T> public delegate void EventHandler <TEventArgs> (object sender, TEventArgs e); // The first object is the event sender as the name suggests, here it must have been Troy. The event // The second parameter is passed. The additional event information we previously defined is public event EventHandler <break up EventArgs>; // define the method for triggering the event to notify the person who has followed the event. Generally, it must be defined as protected and virtual. The former is yes, the latter is necessary for the derived class. protected virtual void On declares that you want to break up (break up EventArgs e) {if (claim to break up! = Null) this. claim that you want to break up (this, e);} public void claim that you want to break up () {// In this step, let's make it clear. var e = new break up EventArgs ("Troy ", "Heart tired"); On claims to break up (e); // If the subclass does not rewrite this event to trigger a function, it will tell everyone who is interested }}

What will happen after the event type is compiled?

C # After the compiler compiles the event, it will convert it into three things: a private delegate field claims to be broken up, and a public event-watching Method add _ claims to be broken up, remove _, a common method for removing interest, claims to break up.

In addition to these three items, the compiler also generates an event definition record item in the metadata of the hosted assembly, it serves only to establish the connection between the abstract concepts of "events" and Its accessors and methods. (Advanced? However, you do not need to understand)

Those who follow the event

All events and events are triggered. The next step is to define the persons who are concerned with the events. The following figures are adapted from real figures:

// The following is the wedding colleague Li public class Lee {public Lee (Troy troy) {// The event is followed by troy during initialization. claim to break up + = this. afterListen;} private void AfterListen (Object sender, break up EventArgs e) {Console. writeLine ("Let troy play games");} // then the single colleague Xiao public class Xiao {public Xiao (Troy troy) {troy. claim to break up + = this. afterListen;} private void AfterListen (Object sender, break up EventArgs e) {Console. writeLine ("indicates Troy hitting single dog");} // because Xiao is not like Lee every day and is traveling with Troy, Xiao may start to hear it and then run away, the private void UnListen (Troy troy) {troy. claim to break up-= this. afterListen ;}}

An object cannot be recycled as long as a method pays attention to the event. So if you want the spam processor to recycle an object, you should stop paying attention to the event.

Okay, that's all.

PS:

This is indeed a real story, just yesterday.

I don't know why I still have the mood to write a blog. I don't know what to do except for this.

This is probably because even if you are out of love, you cannot say you don't want to eat or sleep.

Even more surprising, I don't know why efficiency is so high that I completed my study and blog before.

Everyone needs to grow up, and most of the stories of growth are uncomfortable. Just like staying up late to learn to write a blog, just like breaking up.

If you are grateful for the loss of people, it is more effective than what kind of healing d (Artificial Intelligence) B

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.