C# Learning Diary 24----Event
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:
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics










![Event ID 4660: Object deleted [Fix]](https://img.php.cn/upload/article/000/887/227/168834320512143.png?x-oss-process=image/resize,m_fill,h_207,w_330)
Some of our readers encountered event ID4660. They're often not sure what to do, so we explain it in this guide. Event ID 4660 is usually logged when an object is deleted, so we will also explore some practical ways to fix it on your computer. What is event ID4660? Event ID 4660 is related to objects in Active Directory and will be triggered by any of the following factors: Object Deletion – A security event with Event ID 4660 is logged whenever an object is deleted from Active Directory. Manual changes – Event ID 4660 may be generated when a user or administrator manually changes the permissions of an object. This can happen when changing permission settings, modifying access levels, or adding or removing people or groups

On iPhones running iOS 16 or later, you can display upcoming calendar events directly on the lock screen. Read on to find out how it's done. Thanks to watch face complications, many Apple Watch users are used to being able to glance at their wrist to see the next upcoming calendar event. With the advent of iOS16 and lock screen widgets, you can view the same calendar event information directly on your iPhone without even unlocking the device. The Calendar Lock Screen widget comes in two flavors, allowing you to track the time of the next upcoming event, or use a larger widget that displays event names and their times. To start adding widgets, unlock your iPhone using Face ID or Touch ID, press and hold

When a value is added to the input box, the oninput event occurs. You can try running the following code to understand how to implement oninput events in JavaScript - Example<!DOCTYPEhtml><html> <body> <p>Writebelow:</p> <inputtype="text"

Commonly used events in jquery are: 1. Window events; 2. Mouse events, which are events generated when the user moves or clicks the mouse on the document, including mouse clicks, move-in events, move-out events, etc.; 3. Keyboard events, Events are generated every time the user presses or releases a key on the keyboard, including key press events, key release events, etc.; 4. Form events, such as the focus() event will be triggered when an element gains focus, and the focus() event will be triggered when it loses focus. The blur() event is triggered, and the submit() event is triggered when the form is submitted.

Event processing library in PHP8.0: Event With the continuous development of the Internet, PHP, as a popular back-end programming language, is widely used in the development of various Web applications. In this process, the event-driven mechanism has become a very important part. The event processing library Event in PHP8.0 will provide us with a more efficient and flexible event processing method. What is event handling? Event handling is a very important concept in the development of web applications. Events can be any kind of user row

jQuery is a popular JavaScript library that can be used to simplify DOM manipulation, event handling, animation effects, etc. In web development, we often encounter situations where we need to change event binding on select elements. This article will introduce how to use jQuery to bind select element change events, and provide specific code examples. First, we need to create a dropdown menu with options using labels:

How to implement calendar functions and event reminders in PHP projects? Calendar functionality and event reminders are one of the common requirements when developing web applications. Whether it is personal schedule management, team collaboration, or online event scheduling, the calendar function can provide convenient time management and transaction arrangement. Implementing calendar functions and event reminders in PHP projects can be completed through the following steps. Database design First, you need to design a database table to store information about calendar events. A simple design could contain the following fields: id: unique to the event

In-depth understanding of the close button event in jQuery During the front-end development process, we often encounter situations where we need to implement the close button function, such as closing pop-up windows, closing prompt boxes, etc. When using jQuery, a popular JavaScript library, it becomes extremely simple and convenient to implement the close button event. This article will delve into how to use jQuery to implement close button events, and provide specific code examples to help readers better understand and master this technology. First, we need to understand how to define
