Delegate and event in C,
1 using System; 2 using System. collections. generic; 3 using System. text; 4 5 namespace Delegate 6 {7 // water Heater 8 public class Heater 9 {10 private int temperature; 11 public string type = "RealFire 001 "; // Add the model as demonstration 12 public string area = "China Xian"; // Add the origin as demonstration 13 public delegate void BoiledEventHandler (Object sender, BoiledEventArgs e ); // declare the delegate 14 public event BoiledEventHandler Boiled; // declare the event 15 pub Lic class BoiledEventArgs: EventArgs // defines the BoiledEventArgs class, which is passed to the Observer for information of interest 16 {17 public readonly int temperature; 18 public BoiledEventArgs (int temperature) 19 {20 this. temperature = temperature; 21} 22} 23 protected virtual void OnBoiled (BoiledEventArgs e) // You can override the class inherited from Heater, so that the inheritance class rejects other objects from monitoring it 24 {25 if (Boiled! = Null) 26 {27 Boiled (this, e); // if an object is registered, call the method of all registered objects 28} 29} 30 public void BoilWater () // boil the water. 31 {32 for (int I = 0; I <= 100; I ++) 33 {34 temperature = I; 35 if (temperature> 95) 36 {37 BoiledEventArgs e = new BoiledEventArgs (temperature); // create a BoiledEventArgs object. 38 OnBoiled (e); // call the OnBolied method 39} 40} 41} 42} 43 // alert 44 public class Alarm45 {46 public void MakeAlert (Object sender, Heater. boiledEventArgs e) 47 {48 Heater heater = (Heater) sender; // are you familiar with this? 49 Console. writeLine ("Alarm: {0}-{1}:", heater. area, heater. type); // access the public field 50 Console in sender. writeLine ("Alarm: Tick, water has {0} degrees:", e. temperature); 51 // Console. readLine (); 52 // Console. readKey (); 53 // Console. writeLine (); 54} 55} 56 // display 57 public class Display58 {59 public static void ShowMsg (Object sender, Heater. boiledEventArgs e) 60 {// Static Method 61 Heater heater = (Heater) sender; 62 Console. writeLin E ("Display: {0}-{1}:", heater. area, heater. type); 63 Console. writeLine ("Display: The water is almost burned out. Current temperature: {0. ", E. temperature); 64 // Console. readLine (); 65 Console. readKey (); 66 Console. writeLine (); 67} 68} 69 70 class Program71 {72 static void Main () 73 {74 Heater heater = new Heater (); 75 Alarm alarm = new Alarm (); 76 // heater. boiled + = alarm. makeAlert; // Registration Method 77 // heater. boiled + = (new Alarm ()). makeAlert; // Method for registering anonymous objects 78 // heater. boiled + = new Heater. boiledEventHandler (alarm. makeAlert); // You can also register 79 heater. boiled + = Display. showMsg; // register the static method 80 heater. boilWater (); // The method 81 Console of the registered object is automatically called. readKey (); 82} 83} 84}View Code
1. Use the method as a method parameter
Public void GreetPeople (string name ){
// Do some additional things, such as initialization.
EnglishGreeting (name );
}
Public void EnglishGreeting (string name ){
Console. WriteLine ("Morning," + name );
}
A delegate is a class that defines the type of a method so that the method can be passed as a parameter of another method,
This method is dynamically assigned to parameters to avoid the extensive use of the If-Else (Switch) Statement in the program,
At the same time, the program has better scalability.