Daily improvement-c # basic consolidation, events, and delegation

Source: Internet
Author: User

To improve the technology, I want to understand some problems. I have been engaged in C # development for two years and have been familiar with the concept of event delegation, sometimes, when I look at the articles of other Daniel in the blog Park, I can understand them. After a long time, I forget to understand them. To really understand these concepts, I still need to give some examples by myself, I wrote an article for this purpose and hope to learn and improve with everyone.

Let me take a classic example to illustrate: the host woke up while the mouse was running.

We use the event method to implement it. Call a cat as the method to trigger the event. All methods bound to this event will be triggered.

First, let's use an example on the Internet:

Public class Cat {// <summary> // method of calling the Cat // </summary> public void Miao () {Console. writeLine (""); if (OnMiao! = Null) OnMiao (this, EventArgs. Empty); }/// <summary> // The nickname event // </summary> public event EventHandler OnMiao ;}


Public class Master {public Master () {}/// <summary> // The Master is woken up /// </summary> public void Wake () {Console. writeLine ("What is a dead cat? I'm sleeping !!! ");}}


Public class Mouse {public Mouse () {}/// <summary> // rat Run /// </summary> public void Run () {Console. writeLine ("the cat is here, run !!!! ");}}

Client code

    class Program    {        static void Main(string[] args)        {            Cat cat = new Cat();            Mouse mouse = new Mouse();            Master master = new Master();            cat.OnMiao += delegate { mouse.Run(); };            cat.OnMiao += delegate { master.Wake(); };            cat.Miao();        }    }

This method can be implemented. The cat calls, the mouse runs, and the host wakes up. But if I want to expand it, the host has several cats, each of which has different voices. After the host wakes up, you need to know which cat has woken him up.

Now let's rewrite the Cat class.

/// <Summary> /// Custom Event Data /// </summary> public class CatEventArgs: EventArgs {public string CatName {get; set ;}} public class Cat {// <summary> // defines the event Delegate /// </summary> /// <param name = "sender"> </param> // /<param name = "e"> </param> public delegate void CatEventHandler (object sender, catEventArgs e); public Cat () {}/// <summary> // Cat name /// </summary> public void Miao () {Console. writeLi Ne (""); if (OnMiao! = Null) {CatEventArgs catEventArgs = new CatEventArgs (); catEventArgs. catName = "Tom"; OnMiao (this, catEventArgs) ;}/// <summary> // The completion event of the cat call // </summary> public event CatEventHandler OnMiao ;}

Client code

Class Program {static void Main (string [] args) {Cat cat = new Cat (); Mouse mouse = new Mouse (); Master master Master = new Master (); cat. onMiao + = cat_OnMiao; cat. onMiao + = delegate {master. wake () ;}; cat. miao ();} static void cat_OnMiao (object sender, CatEventArgs e) {Console. writeLine (string. format ("{0} the cat is calling", e. catName ));}}

In this way, we know which cat is called. The key is that we have customized CatEventArgs. This class inherits EventArgs and can pass event information and custom information, custom events delegate public delegate void CatEventHandler (object sender, CatEventArgs e );

Let's take a look at your own small example. In the new year, I wish you a better chance.

-- Daily Improvement

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.