C # Learning Diary----Events (event)

Source: Internet
Author: User
The event provides the ability to send notifications to the outside world for instances of classes and classes, implements communication between objects and objects, and if an event member is defined, indicates that the type has 1. Ability to register a method in an event (+ = operator implementation). 2. Ability to unregister the method in an event (-= operator implementation). 3. The method of registration is notified when an event is triggered (a list of registration methods is maintained within the event). A delegate (Delegate) is the carrier of an event, and a delegate is needed to define the event. For the contents of the delegate, click on the delegate (Delegate) to enter the understanding.

Declare an event:

When declaring an event inside 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 example above, then declare the event, using the keyword event

                  Pulic event Mydelegatehandler MyEvent;

(The object type is the base class for all classes, and the details about him have been mentioned earlier. Click the object type to understand that EventArgs is the base class for the class that contains the event data that is used to pass the details of the event. )

Write an instance of the event:

Every week in Saturday I have a shine habit, like to go to school outside a supermarket to buy things, that supermarket has an automatic door, that is, when we approached a certain distance (3 meters) will automatically open, but also very gentle and cordial said "welcome", because I often go to his home to buy things, to deal with the member, So whenever I approached the automatic door seems to know me, very enthusiastic to say "warmly welcome HC666 visit this supermarket ^_^" this door is quite fun that

In the example above, the "Auto Gate" is treated as an object instantiated by door, "I" is an object instantiated by person, when I call "go to the supermarket" action, and 3 meters away from the supermarket gate, triggering our definition of "Enterdoor" event, However, Enterdoor inside uses the commission to register an "automatic Door (door)" "Open Door (opendoor)" action, equivalent to call the open door method, so that the object and object to communicate and communicate, the code is as follows:

Using System;  Using System.Collections.Generic;  Using System.Linq;      Using System.Text;          namespace Test {//defines a person class that contains the method class person {public string name = "HC666";          private int distance;          Declaration delegate public delegate void Enterdoorhandler (Object Sender,enterdoorargs e);          Declaring events public event Enterdoorhandler Enterdoor based on delegate declaration;  Defines a way to go to the supermarket when the distance distance<=3 triggers the event public void Gotostore () {for (int i = 6; i > 0;                  i--) {distance = i;                      if (I <= 3) {//trigger event Enterdoorargs e = new Enterdoorargs (distance); Onenterdoor (e);//Call Trigger Event Method}}} public void Onenterdoor          (Enterdoorargs e)              {//Invoke the event to register the method if (Enterdoor! = null) Enterdoor (this, e); else Console.WriteLine ("No processing Method added"); }//define a class containing event data, here distance is a critical data for judging public class Enterdoorargs:eventargs {public I             NT distance;             public Enterdoorargs (int distance) {this.distance = distance; }}}//define gate This class Door {//Defines the method of opening the door public void Opendoor (object sender, person.  Enterdoorargs e) {person per = (person) sender; A little familiar, the display type conversion has talked about if (e.distance = = 3) {Console.WriteLine ("Dear Customer you are from the supermarket {0} meters              will open the door to greet you ", e.distance);                        } if (E.distance <3) Console.WriteLine ("Warmly welcome {0} to visit the supermarket", Per.name); }} class Program {static void Main (string[] args) {person per = new per Son ();              Instantiate object Door Door = new Door (); The method of registering the door to the event per. Enterdoor + = door.    Opendoor;          I go to the supermarket per.            Gotostore (); }      }  }

Results:

The above is the C # learning Diary----Event content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.