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