Delegate (5) Delegate and event, delegate event

Source: Internet
Author: User

Delegate (5) Delegate and event, delegate event

During the interview, I am often asked about the connection and difference between delegation and events? I haven't fully understood it before. Let's summarize it below.

Start with an interesting requirement. There are three roles: CAT, mouse, and host. When the cat is called, the mouse starts to escape, and the host wakes up from his sleep.

Event implementation

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 Master = new Master ("Zhang San", cat); 10 // cat call, notify all subscribers 11 cat. catCry (); 12 13 Console. readKey (); 14} 15} 16 17 # region cat 18 public class Cat19 {20 private string name; 21 22 // declare event 23 public event EventHandler <CatCryEventArgs> CatCryEvent; 24 25 pu Blic Cat (string name) 26 {27 this. name = name; 28} 29 30 public void CatCry () 31 {32 // declare the event parameter 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 parameter 43 // </summary> 44 public class CatCryEventArgs: EventArgs45 {46 private string catName; 47 48 public CatCryEventArgs (string catName) 49: base () 50 {51 th Is. catName = catName; 52} 53 54 public override string ToString () 55 {56 return string. format ("{0} called", catName); 57} 58} 59 # endregion60 61 # region rat 62 public class Mouse63 {64 private string name; 65 public Mouse (string name, Cat cat) 66 {67 this. name = name; 68 cat. catCryEvent + = CatCryEventHandler; // In essence, add a method 69} 70 71 to the delegate chain // event handler 72 private void CatCryEventHandler (object sender, CatCryEventArgs e) 73 {74 Console. WriteLine ("{0} escaped: I'm leaving. Hurry up! ", Name); 75} 76} 77 # endregion78 79 # region Master 80 public class Master81 {82 private string name; 83 public Master (string name, Cat cat) 84 {85 this. name = name; 86 cat. catCryEvent + = CatCryEventHandler; // Add a method 87} 88 89 in essence to the delegate chain // event handler 90 private void CatCryEventHandler (object sender, CatCryEventArgs e) 91 {92 Console. writeLine ("{0} woke up: Let's go and call it a hammer! ", Name); 93} 94} 95 # endregion96 97}

 

The demo can be summarized as follows:

1. Define and use the event process, such:

2. Define event parameters to inherit EventArgs, and define events to use publicEvent EventHandler<CatCryEventArgs> CatCryEvent;

3. Events use the observer mode, including publishing, subscription, and notification. The essence of the implementation is summarized below.

Delegate implementation
1 namespace ConsoleApplication5 2 {3 // declare delegate 4 public delegate void Del1 (); 5 6 class Program 7 {8 static void Main (string [] args) 9 {10 // create a delegate chain (chain delegate) 11 Del1 del1 = () => Console. writeLine ("cat called"); 12 del1 + = () => Console. writeLine ("the mouse has escaped: I'm leaving. Hurry up! "); 13 del1 + = () => Console. WriteLine (" the master woke up: Let me go and call it a hammer! "); 14 15 // call delegate 16 del1 (); 17 18 Console. ReadKey (); 19} 20 21} 22}

 

We can see that it is actually a chain delegate call. Three methods are added to the chain delegate and executed in sequence during the call.

Events and Delegation

To thoroughly understand the relationship between events and delegation, let's look at the source code of EventHandler, as shown in.

Are you marked in red? Therefore, events are implemented based on delegation. Summary:

Contact:

1. Events are implemented based on delegation and can be easily understood:An event is a Special Delegate. In particular, it defines a delegate with two parameters (event source and event parameter) without return values.

2,When the subscriber of an event subscribes to an event, it essentially adds the event processing method to the delegate chain.When an event is triggered, all event handling methods in the delegate chain are called.

 

Differences:

The nature of delegation is a type of custom (class), and the nature of events is a special delegate instance (object ).

 

Related Article

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.