Home  >  Article  >  Backend Development  >  C# Learning Diary 24----Event

C# Learning Diary 24----Event

黄舟
黄舟Original
2017-01-21 15:28:471524browse

Events provide classes and class instances with the ability to send notifications to the outside world, realizing communication between objects. If an event member is defined, it means that the type has 1. The ability to register methods in the event (+ = operator implementation). 2. Ability to unregister methods in events (-= operator implementation). 3. The registered method will be notified when the event is triggered (the event maintains a list of registered methods internally). Delegate is the carrier of event. To define an event, you must have a delegate. For more information about delegation, please click Delegate to learn more.

State an event:

Internal declaration of the event, first of all, you must declare the commission type of the incident. 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, details about it have been mentioned before After clicking on the object type, EventArgs is the base class of the class that contains event data and is used to pass event details)

Write an event instance:

I have an unshakable habit every Saturday. I like to go to a supermarket outside the school to buy things. That supermarket has an automatic door that will automatically open when we approach a certain distance (3 meters). He will say "Welcome" very gently and kindly. Because I often go to his home to buy things and have signed up as a member, the automatic door seems to recognize me every time I approach, and he will say "Warmly welcome HC666 to our supermarket" very warmly. ^_^"This door is quite interesting.

In the above example, the "automatic door" is regarded as an object instantiated by Door, and "I" is an object instantiated by person. When I call The action of "going to the supermarket" and being 3 meters away from the supermarket door triggers the "Enterdoor" event we defined. However, Enterdoor uses a delegate to register the "Opendoor" action of an "automatic door (door)", which is quite Because the door opening method is called, communication and exchange between objects are achieved. The code is as follows:

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
  
  
namespace Test  
{ //定义一个person类,里面包含了方法  
    class person   
    {  
        public string name = "HC666";  
        private int distance;  
        //声明委托  
        public delegate void EnterdoorHandler(object sender,EnterdoorArgs e);  
        //基于委托声明事件  
        public event EnterdoorHandler Enterdoor;  
        //定义的一个去超市的方法,当距离 distance<=3的时候触发事件  
        public void GotoStore()  
        {  
            for (int i = 6; i > 0; i--)  
            {  
                distance = i;  
                if (i <= 3)  
                {//触发事件了  
                    EnterdoorArgs e = new EnterdoorArgs(distance);  
                    OnEnterdoor(e);//调用触发事件方法  
                }  
            }  
          
        }  
        public void OnEnterdoor(EnterdoorArgs e)  
        {//调用事件里注册的方法  
            if (Enterdoor != null)  
                Enterdoor(this, e);  
            else  
                Console.WriteLine("没有添加处理方法");          
        }  
        //定义一个包含事件数据的类,这里distance是一个判断的重要数据  
       public class EnterdoorArgs:EventArgs  
       {  
           public int distance;  
           public EnterdoorArgs(int distance)  
           {  
               this.distance = distance;  
           }  
       }  
    }  
    //定义门这个类  
    class Door  
    {//定义开门的方法  
        public void Opendoor(object sender, person.EnterdoorArgs e)  
        {  
            person per = (person)sender;  //有点熟悉吧,显示类型转换中有谈到  
            if (e.distance == 3)  
            {  
                Console.WriteLine("尊敬的顾客您距离本超市 {0}米 即将开门迎接您", e.distance);  
            }  
            if(e.distance <3)  
                Console.WriteLine("热烈欢迎 {0} 光临本超市", per.name);  
              
        }  
    }  
    class program  
    {  
        static void Main(string[] args)  
        {  
            person per = new person(); //实例化对象  
            Door door = new Door();  
            //向事件中注册开门的方法  
            per.Enterdoor += door.Opendoor;  
            //我去超市  
            per.GotoStore();  
  
        }  
    }  
}

Result:

C# Learning Diary 24----Event

The above is C# The content of Learning Diary 24----Event, please pay attention to the PHP Chinese website (www.php.cn) for more related content!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn