C # event mechanism (remember the process)

Source: Internet
Author: User
Tags textout
From: http://jimmyloveforever.blog.163.com/blog/static/119775247200951303935836/ 1, C # The event mechanism is based on the delegate implementation, so we need to first define a delegate eventhandler:
Public Delegate void eventhandler (object from, myeventargs e) system. eventargs is the base class of the class containing event data. You can directly use the eventargs class in the code. The myeventargs class is derived from the eventargs class to implement the function of customizing event data. Here, from indicates the event object.
2. Define the event format as follows:
The delegate name of an event, such as the event textout definition:
Public event eventhandler textout;
3. Event activation is generally written as follows:
If (textout! = NULL)
Textout (this, new eventargs ());
Check whether the textout event has been subscribed. If it is not null, a user has subscribed to the event. Which of the following events is subscribed?
Testapp class, first instantiate eventsource, and then subscribe to the event:
Evsrc. textout + = new eventsource. eventhandler (catchevent );
You can also cancel the subscription:
Evsrc. textout-= new eventsource. eventhandler (catchevent );

View code

Using system; using system. collections. generic; using system. LINQ; using system. text; // define the event containing the data public class myeventargs: eventargs {private string strtext; Public myeventargs (string strtext) {This. strtext = strtext;} Public String getstrtext {get {return strtext ;}}// class eventsource {myeventargs evargs = new myeventargs ("trigger event") of the event publishing class "); // define the delegate Public Delegate void eventhandler (Object sender, myeventargs E); // define the event public event eventhandler textout; // The method for activating the event public void triggerevent () {If (textout = NULL) textout (this, evargs) ;}// class testapp of the subscribed event {public static void main () {eventsource evsrc = new eventsource (); // subscribes to the evsrc event. textout + = new eventsource. eventhandler (catchevent); // triggers the evsrc event. triggerevent (); console. writeline ("------"); // cancels the subscription event evsrc. textout-= new eventsource. eventhandler (catchevent); // triggers the evsrc event. triggerevent (); console. writeline ("------"); // The Event subscription has been canceled and testapp theapp = new testapp (); evsrc is not executed. textout + = new eventsource. eventhandler (theapp. instancecatch); evsrc. triggerevent (); console. writeline ("------");} // static method for processing events public static void catchevent (object from, myeventargs e) {console. writeline ("catchevent: {0}", E. getstrtext);} // public void instancecatch (object from, myeventargs e) {console. writeline ("instancecatch: {0}, E. getstrtext ");}}

 

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.