During interviews, people are often asked about the connections and differences between delegation and events. I haven't fully understood it before, so let's summarize it.
Start with an interesting need. There are three characters, cat, mouse and owner. When the cat barks, the mouse starts to run away and the owner wakes up from his sleep.
Using Event Realization
The following code:
1 namespace ConsoleApplication4 2 { 3 class Program 4 { 5 static void Main(string[] args) 6 { 7 Cat cat = new Cat("cat"); 8 Mouse mouse1 = new Mouse("Mouse", cat); 9 Master master = new Master("Zhang San", cat); 10 //Cat barks, notify all subscribers 11 cat.CatCry(); 12 13 Console.ReadKey(); 14 } 15 } 16 17 #region cat 18 public class Cat 19 { 20 private string name; 21 22 //Declaring events 23 public event EventHandler<CatCryEventArgs> CatCryEvent; 24 25 public Cat(string name) 26 { 27 this.name = name; 28 } 29 30 public void CatCry() 31 { 32 //Declare event parameters 33 CatCryEventArgs args = new CatCryEventArgs(name); 34 Console.WriteLine(args); 35 36 //Trigger event 37 CatCryEvent(this, args); 38 } 39 } 40 41 /// <summary> 42 /// Event parameters 43 /// </summary> 44 public class CatCryEventArgs : EventArgs 45 { 46 private string catName; 47 48 public CatCryEventArgs(string catName) 49 : base() 50 { 51 this.catName = catName; 52 } 53 54 public override string ToString() 55 { 56 return string.Format("{0}Called", catName); 57 } 58 } 59 #endregion 60 61 #region Mouse 62 public class Mouse 63 { 64 private string name; 65 public Mouse(string name, Cat cat) 66 { 67 this.name = name; 68 cat.CatCryEvent += CatCryEventHandler;//Essentially, adding a method to the delegation chain 69 } 70 71 //Event Handler 72 private void CatCryEventHandler(object sender, CatCryEventArgs e) 73 { 74 Console.WriteLine("{0}Run away: I'll pull up and run!", name); 75 } 76 } 77 #endregion 78 79 #region master 80 public class Master 81 { 82 private string name; 83 public Master(string name, Cat cat) 84 { 85 this.name = name; 86 cat.CatCryEvent += CatCryEventHandler;//Essentially, adding a method to the delegation chain 87 } 88 89 //Event Handler 90 private void CatCryEventHandler(object sender, CatCryEventArgs e) 91 { 92 Console.WriteLine("{0}Wake up: I'll pull up and call a hammer!", name); 93 } 94 } 95 #endregion 96 97 }
Through demo, we can conclude that:
1. Define and use the flow of events as follows:
2. Define event parameters to inherit EventArgs and use public event EventHandler < CatCryEventArgs > CatCryEvent to define events.
3. Events use the observer model, with publishing, subscribing and notification. As for how to achieve this, what is the essence is summarized below.
Using delegated implementation
1 namespace ConsoleApplication5 2 { 3 //Statement of Entrustment 4 public delegate void Del1(); 5 6 class Program 7 { 8 static void Main(string[] args) 9 { 10 //Create a delegation chain(Chain delegation) 11 Del1 del1 = () => Console.WriteLine("The cat barked."); 12 del1 += () => Console.WriteLine("The rat ran away: I'll pull it up and run!"); 13 del1 += () => Console.WriteLine("The master woke up: I'll pull up and call a hammer!"); 14 15 //Call delegation 16 del1(); 17 18 Console.ReadKey(); 19 } 20 21 } 22 }
As you can see, it's actually a chain delegation call. Three methods are added to the chain delegate, which is executed in turn when invoked.
Events and delegations
To get a thorough understanding of the relationship between events and delegations, let's look at the source code of EventHandler, as shown below.
See the red marking in the picture above? So events are delegated. To sum up:
Contact:
1. Events are based on delegation, which can be understood as: Events are a special delegation. The special point is that they define a delegation with two parameters (event source and event parameter) and no return value.
2. When event subscribers subscribe to events, they essentially add event handling methods to the delegation chain. When events trigger, all event handling methods in the delegation chain are invoked.
Difference:
The delegation essence is a kind of custom type, and the event essence is a special delegation instance (object).