C # event implementation

Source: Internet
Author: User

Events are an important part of C #. There is a demo of custom events on MSDN. After reading it for a long time, I was a little dizzy. So I created a new winform project and added a button to find the program to be called. I made a similar one-to-one comparison example and I understood it. Reading code is sometimes faster than reading a document. Therefore, it is a consistent principle. [Csharp] using System; namespace TestEventArgs {// <summary> // This class corresponds to EventArgs for comparative learning. /// Add info1 and info2. /// </Summary> public class MyEventArgs: EventArgs {private String info1; private UInt32 info2; public MyEventArgs (String info1, UInt32 info2) {this. info1 = info1; this. info2 = info2;} public String Info1 {get {return this. info1;} set {this. info1 = value ;}} public UInt32 Info2 {get {return this. info2;} set {this. info2 = value ;}}/// <summary> /// simulate the Button /// </summary> public Class MyButton {public delegate void MyEvnetHandler (object sender, MyEventArgs e ); /// <summary> /// counter of the number of Button Clicks /// </summary> public static UInt32 clicked_num = 0; public event MyEvnetHandler MyClick; public void trigger () {MyEventArgs arg = new MyEventArgs (DateTime. utcNow. toString (), ++ clicked_num); MyClick (this, arg );}} /// <summary> /// simulate Form /// </summary> public class MyForm {public MyBut Ton button; public MyForm () {button = new MyButton (); button. myClick + = new MyButton. myEvnetHandler (this. button_Clicked);} public void button_Clicked (object sender, MyEventArgs e) {Console. writeLine ("button clicked (sender is:" + sender. toString () + "; info1 =" + e. info1 + "; info2 =" + e. info2) ;}} class Program {static void Main (string [] args) {MyForm form = new MyForm (); for (int I = 0; I <10; I ++) {Www.2cto.com form. button. trigger (); System. threading. thread. sleep (500);} Console. writeLine ("Press any key to continue... "); Console. readKey () ;}} is different: 1 in this example, delegate myevnethandler is a member of the mybutton class. In the system, eventhander is a member of the system namespace.

Related Article

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.