What is an event?
An event is a member of a class.If the type defines an event, it can notify other objects of a specific event (such as the Click Event of the button ). An event is a member of the interaction type.
Events are based on the delegation,Events are encapsulated delegates.
I. publisher and subscriber Modes
Understanding this figure is very important for understanding the principles of events:
1. The publisher class defines the event members.
2. subscriber class registers the method to be called when an event member is triggered (Event ProcessingProgram).
3. When the publisher triggers an event, all event handlers in the list will be called.
Ii. Actual cases
This is an example of email Arrival notification. When an email arrives, A newmail event will be triggered, and both fax and pager registered to this event will receive notifications, and process the email in your own way.
CodeAs follows:
Namespace Eventdemo2 { Class Program { Static Void Main ( String [] ARGs) {mailmanager mm = New Mailmanager (); fax Fax = New Fax (mm); pager Pager = New Pager (mm); mm. simulatenewmail ( " A " , " B " , " Hi, how are you? " ); Console. writeline (); console. writeline ( " * ******* Pager undo register ******** " ); Pager. unregister (mm); mm. simulatenewmail ( " B " ," A " , " I'm fine, and you? " ); Console. readkey ();}} // Publisher class Internal Class Mailmanager { // 1. Declare the event Public Event Eventhandler <newmaileventargs> Newmail; // 2. trigger event 1-Method of triggering event Protected Virtual Void Onnewmail (newmaileventargs e ){ If (Newmail! = Null ) {Newmail ( This , E );}} // 3. trigger event 2-convert input to expected event Public Void Simulatenewmail ( String From , String To, String Subject) {newmaileventargs E = New Newmaileventargs ( From , To, subject); onnewmail (e );}} // Custom class, which transmits data by extending eventargs // The information sent to the event recipient is contained here. Internal Class Newmaileventargs: eventargs { Private Readonly String _ From, _ to, _ subject; Public Newmaileventargs ( String From , String To, String Subject) {_ from =From ; _ = To; _ subject = Subject ;} Public String From { Get { Return _ From ;}} Public String To { Get {Return _ ;}} Public String Subject { Get { Return _ Subject ;}}} // Subscriber Class 1 Internal Class Fax { Public Fax (mailmanager mm) {mm. newmail + = New Eventhandler <newmaileventargs> (faxmsg ); // Register an event handler // Mm. newmail + = faxmsg; // Equivalent to the above } Public Void Unregister (mailmanager mm) {mm. newmail -= New Eventhandler <newmaileventargs> (faxmsg ); // Cancel event handler } Private Void Faxmsg ( Object Sender, newmaileventargs E) // Note: The return type and signature of the handler must be consistent with the return type and signature of the event Delegate. {Console. writeline ( " Faxing mail message: " ); Console. writeline ( " From: {0}, To = {1}, subject = {2} " , E. From, E. To, E. Subject );}} // Subscriber Category 2 Internal Class Pager { Public Pager (mailmanager mm) {mm. newmail + = Pagermsg; // Register an event handler } Public Void Unregister (mailmanager mm) {mm. newmail -= Pagermsg; // Cancel event handler } Private Void Pagermsg ( Object Sender, newmaileventargs e) {console. writeline ( " Pager mail message: " ); Console. writeline ( " From: {0}, To = {1}, subject = {2} " , E. From, E. To, E. Subject );}}}
Shows the program output result.
Note: 1, + = and-= are the only operators allowed to be used in an event, representing the registration of the event handler and the cancellation of the event handler.
2. Do not mistakenly think that an event is a type, and an event is a type member. Therefore, it cannot use an object creation expression (new expression) to create its object.
3. The call event is similar to the call delegate (similar to the call method), but note that its parameters must match the event Delegate.
4. There are multiple methods to subscribe to events (register an event handler), such as the instance method, static method, anonymous method, or Lambda expression. The most common method is to use a delegated instance. For example:
BTN. Click + = new eventhandler (btn_click); // equivalent to: BTN. Click + = btn_click;
5. eventargs, the second parameter of the eventhandler delegate, cannot transmit any data by default. To be designed to transmit data, you must customize a class inherited from eventargs, use private fields to save the data to be passed.