C # Custom Events

Source: Internet
Author: User

The concept of events has been revisited today, and the understanding of events has been more profound. The final event is a special delegate. When we define an event, once we trigger the event, it means that we are invoking a series of methods behind it. We know that a delegate or an anonymous function (an anonymous method and a lambda expression) is a delegate that passes a method parameter to meet our requirements. If we have to deal with a function that requires some sort of action to be done, it means that we need to pass a number of methods, and we have to do it in a certain order, we can use the event. In other words, once we have bound a series of methods of an event, when the event is triggered, some of the methods will be executed in a smooth succession. In order to better complete the function.

Needless to say, I believe that programmers have a sense of the code of intimacy, see the code more to appreciate its beauty. Here are a few lines of short code to illustrate the problem:

     public partial class Form1 : Form     {        public form1 ()          {            initializecomponent ();             //subscription events, binding with two methods (Message1 and Message2)             smg += new showmessage ( Message);            smg += new  ShowMessage (Message1);                    }        //declares a delegate that specifically delivers messages smg         public event showMessage smg;                 private void message ()          {            messagebox.show ("What's Your Name");         }        private  Void message1 ()         {             messagebox.show ("My name is Xiao Kai");         }        private void button1_click (Object sender ,  eventargs e)         {          //directly invokes the event, triggering the SMG event. Complete the execution of the two sentences.             SMG ();         }

Custom events are as simple as this. The key still can be applied to the actual ... Come on!

C # Custom Events

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.