C # Learning diary 24 ---- events)

Source: Internet
Author: User

C # Learning diary 24 ---- events)

The event provides the ability to send notifications to the outside world for instances of classes and classes to implement communication between objects. If an event member is defined, it indicates that this type has 1. the ability to register methods (+ = Operator Implementation) in events ). 2. The method can be deregistered in the event (-= Operator implementation ). 3. When an event is triggered, the registration method will be notified (the event maintains a registration method list ). A Delegate is the carrier of an event. to define an event, a Delegate is required. For more information about delegation, clickDelegate)Go to learn more.

 

Declare an event:

To declare an event within a class, you must first declare the delegate type of the event. For example:

Pulic delegate void MyDelegateHandler (object sender, EventArgs e );

Then, based on the above example, declare the event and use the keyword event

Pulic event MyDelegateHandler MyEvent;

(The object type is the base class of all classes. For more information about the object type, seeObject TypeTo learn more, EventArgs is the base class of the class containing event data, used to pass event details .)

 

Write an event instance:

On Saturday every week, I have a habit of not moving. I like to go to a supermarket outside the school to buy things. That supermarket has an automatic door, that is, when we approach a certain distance (3 meters) it will be opened automatically, and it will be very gentle and kind to say "welcome", because I often go to his house to buy things and handle membership, so every time I approached, the automatic door seemed to know me and said with great enthusiasm, "warmly welcome HC666 to our supermarket ".

In the above example, the "Automatic Door" is treated as an object instantiated by Door, and "I" is an object instantiated by person. When I call the "go to supermarket" action, the "Enterdoor" event we defined is triggered 3 meters away from the supermarket door. However, Enterdoor uses the "Open door" event that is delegated to register a "door) "action, which is equivalent to calling the door opening method, so as to achieve communication and communication between objects. The Code is as follows:

 

Using System; using System. collections. generic; using System. linq; using System. text; namespace Test {// defines a person class, which contains the method class person {public string name = HC666; private int distance; // declare the delegate public delegate void EnterdoorHandler (object sender, EnterdoorArgs e); // a way to go to the supermarket defined based on the delegate declaration event public event EnterdoorHandler Enterdoor, when the distance from distance is <= 3, the event public void GotoStore () {for (int I = 6; I> 0; I --) {Distance = I; if (I <= 3) {// trigger the event EnterdoorArgs e = new EnterdoorArgs (distance); OnEnterdoor (e ); // call the trigger event Method }}public void OnEnterdoor (EnterdoorArgs e) {// call the method registered in the event if (Enterdoor! = Null) Enterdoor (this, e); else Console. writeLine (no processing method added);} // defines a class containing event data. Here distance is an important data judgment public class EnterdoorArgs: EventArgs {public int distance; public EnterdoorArgs (int distance) {this. distance = distance ;}}// defines the class Door of the Door. {// defines the method for opening the Door. public void Opendoor (object sender, person. enterdoorArgs e) {person per = (person) sender; // you may be familiar with it. if (e. distance = 3) {Console. writeLine (dear customer, you are about to open the door {0} meters away from the supermarket to meet you, e. distance);} if (e. distance <3) Console. writeLine (warmly welcome {0} to our supermarket, per. name) ;}} class program {static void Main (string [] args) {person per = new person (); // instantiate the object Door door = new Door (); // register the method for opening the door in the event per. enterdoor + = door. opendoor; // I will go to the supermarket per. gotoStore ();}}}


Result:

 

 

      

 

 

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.