Summarize the connections and differences between delegates and events

Source: Internet
Author: User
In the interview is often asked, the relationship between the Commission and events and the difference? Has not been thoroughly understood before, to summarize below.

Start with an interesting demand. There were three characters, the cat, the mouse and the master, and when the cat called, the mouse began to flee, and the master awakened from his sleep.

Using event implementations

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 call, notify all subscribers of cat.         Catcry (); Console.readkey ()}15}16 #region Cat public class Cat19 {20         private string name;21 22//declaring event Eventhandler<catcryeventargs> public event catcryevent;24 25 Public Cat (string name)-{this.name = name;28}29-public void Catcry () 3 1 {32//declaration event parameter Catcryeventargs args = new Catcryeventargs (name); CONSOLE.W Riteline (args); 35 36//Trigger Event PNS Catcryevent (this, args);}39}40//<summar Y&GT;42///&LT;/SUMMARY&GT;44 public class Catcryeventargs: EVENTARGS45 {catname;47-private string Catcryeventargs (string catname) 49:b         ASE () {this.catname = catname;52}53-public override string ToString () 55 {The return string.         Format ("{0} called", catname);}58}59 #endregion60 #region Mouse, public class Mouse63 {64             private string name;65 public Mouse (string name, Cat cat), {this.name = name;68 Cat. Catcryevent + = catcryeventhandler;//is essentially adding a method to the delegate chain 69}70 71//event handler private void Catcryeventh Andler (object sender, Catcryeventargs e) Console.WriteLine ("{0} escaped: I'll go and run!" ", name);}76}77 #endregion78 #region Master of the public class Master81 {the ing name;83 public Master (string name, Cat cat) $ {this.name = name;86 Cat. CatCRyevent + = catcryeventhandler;//is essentially adding a method to the delegate chain 87}88 89//event handler + private void Catcryeventhandl Er (object sender, Catcryeventargs e) Console.WriteLine ("{0}" woke up: I'll go and call a hammer! ", name);}94}95 #endregion96 97}

You can summarize by demo:

1, the process of defining and using events, such as:

2, define event arguments to inherit EventArgs, define events using public event EventHandler<CatCryEventArgs> catcryevent;

3, the event uses the Observer pattern, there are publications, subscriptions and notifications, as to how to achieve, what is essentially what is summarized below.

Using delegates to implement

1 namespace ConsoleApplication5 2 {3     //Declaration Delegate 4 Public     delegate void Del1 (); 5  6     class program 7     {8
  static void Main (string[] args) 9         {Ten             //Create a delegate chain (chained delegate) one             Del1 Del1 = () = Console.WriteLine ("Cat barks"); 12< C11/>del1 + = () = Console.WriteLine ("The rat escaped: I'll go and run!" ");             Del1 + = () = Console.WriteLine (" The Master woke up: "I'll go and call a hammer! "); [             Del1] ();             Console.readkey ();         }20     }22}

As you can see, it is actually a chain delegate invocation. Three methods were added to the chained delegate and executed sequentially when called.

Events and Delegates

To figure out the relationship between the event and the delegate, we look at the source code for the next eventhandler, such as.

Did you see the red mark? Therefore, events are implemented based on delegates. To summarize:

Contact:

1, the event is based on the implementation of the delegate, can be popularly understood as: The event is a special delegate, the special place is that it defines a two parameters (event source and event arguments) have no return value of the delegate.

2, when the subscriber of an event subscribes to an event, it essentially joins the event's processing method into the delegate chain , and when the event fires, all the event-handling methods in the delegate chain are called.

Difference:

The essence of a delegate is a custom type (class), and the event essence 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.