C # Delegate Events

Source: Internet
Author: User

  

1 namespaceConsoleApplication62 {3     class Program4     {5         Static voidMain (string[] args)6         {   7             //instantiating delegate Classes8MYAPP app=NewMyApp ();9             //the method to be executed by the event bindingTenApp. Presskeyboard + =Stratrun; One              while(true) A             { -                 //declare the value of the keyboard pressed -                 intOnKey; the                 //try converting the string that is pressed by the keyboard to the int number type. Failed to return 0; -                 int. TryParse (Console.ReadLine (), outOnKey); -                 //the method that triggered the event (the total number of incoming and keyboard pressed values) - app. Onpresskeyboard (app. Count,onkey); +                 //Print Total Value -Console.WriteLine ("The total value is:"+app. Count); +             } A             at  -         } -         /// <summary> -         ///To create an execution method -         /// </summary> -         /// <param name= "Count" ></param> in         /// <param name= "num" ></param> -         /// <returns></returns> to         Static intStratrun (intCountintnum) = + count +num; +  -     } the     /// <summary> *     ///The first step declares the delegate $     /// </summary>Panax Notoginseng     /// <param name= "Count" >Total before calculation</param> -     /// <param name= "num" >Digital</param> the     /// <returns>the total number after calculation</returns> +      Public Delegate intPresskeyboardeventhandler (intCountintnum); A  the      Public classMyApp +     {    -         /// <summary> $         ///Total $         /// </summary> -          Public intCount =0; -         //The second step declares the event instantiation delegate the          Public EventPresskeyboardeventhandler Presskeyboard; -         /// <summary>Wuyi         ///Create a method trigger event the         /// </summary> -         /// <param name= "Count" ></param> Wu         /// <param name= "num" ></param> -         /// <returns></returns> About          Public Virtual voidOnpresskeyboard (intCountintnum) $         { -             varInvoke = Presskeyboard?. Invoke (count, num); -             if(Invoke! =NULL) -             { ACount = (int) invoke; +             } the         } -     } $}

//delegate is a type.
the//event is an instance of a delegate.

//1. Declaring a delegate
//2, declaring an event instantiation delegate
//3. Creating a method to trigger events
//4. Creating a binding method for event execution
//5. Instantiating the class of the delegate
//6. Bind the method that triggered the event to the event
//7. Executing a Trigger event method

C # Delegate 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.