Event. MSDN explains: classes or objects can notify other classes or objects of related events through events. The class for sending (or triggering) events is called "publisher", and the class for receiving (or processing) events is called "subscription ". We will not talk nonsense about the theory and benefits of events here. If you are interested, please refer to the MSDN explanation. Here is a demo to demonstrate how to implement standard event processing step by step. It can be used as a reference for writing events in future code. To use events, you need to know about delegation and events. You can go to relevant tutorials. The simulation has a horrible arms provider asking us to implement a Rocket Gun order management program. : Step 1: Let's take a look at the event description on MSDN in basic practice. We may want to practice it to compile a simple event processing, which is neither parameter nor standard, but the event implementation function has been achieved. Code
1 publicclass BookBomb
2 {
3 /// <summary>
4 // define Delegation
5 /// </summary>
6 publicdelegatevoid BookBombs ();
7 /// <summary>
8 // define an event
9 /// </summary>
10 publicevent BookBombs Book;
11 publicevent BookBombs Booking;
12 publicevent BookBombs Booked;
13
14 /// <summary>
15 // reservation
16 /// </summary>
17 publicvoid OnBook ()
18 {
19 if (Book! = Null)
20 {
21 Book ();
22}
23}
24 /// <summary>
25 // Processing
26 /// </summary>
27 publicvoid OnBooking ()
28 {
29 if (Booking! = Null)
30 {
31 Booking ();
32}
33}
34 /// <summary>
35 // Assembly completed
36 /// </summary>
37 publicvoid OnBooked ()
38 {
39 if (Booked! = Null)
40 {
41 Booked ();
42}
43}
44}
45}
This is an example of a very basic and non-standard event. It realizes three steps: Order booking, assembly processing, and assembly. Registering an event is simple. After the event name is entered + =, you will be prompted to press the Tab key to automatically import the corresponding processing programs for each event.