"Turn" C # Delegation of Notes (Delegate) (i)

Source: Internet
Author: User

Original: Small base to learn C # notes commissioned (Delegate) (I )

Originally because of the previous interface that article many altar friends gave some advice, spent a morning event code of the interface and abstract class comparison and understanding, who know ... Who knows... No, the wrong number is the verification code, Wood has all the wood?! Recover only a title good Sad ~ to come again, today is really not that mood, clean up my wounded heart, change the topic, abstract class just wait for the next time! All right, here we go!

first of all, we understand what is the use of the Commission. My understanding is, I find someone to help me work, hey ~ don't understand? When I give you a scene, one is my family did not ask for a nanny, one is I have a nanny. [Code]csharpcode:
Using system;using system.collections.generic;class Xiaojian//write a "small cheap" class, hey, that is me, look at the following there are so much work to do, now I have not asked the nanny {public    String washthedishes ()//dishwashing T. T    {        return "wash dishes ... ";    }    public string Cleanthefloor ()//sweep T. T    {        return "sweep sweeping ... ";    }    public string washtheclothes ()//wash clothes T. T    {        return "laundry laundry ... ";    }}
[Code]csharpcode:
Using system;using system.collections.generic;class dohousework//This class is doing housework {    static void Main (string[] args)    {        Xiaojian Xiaojian = new Xiaojian ();//This is me, I can see all the following work is I am doing        Console.WriteLine (Xiaojian.washthedishes ());        Console.WriteLine (Xiaojian.cleanthefloor ());        Console.WriteLine (Xiaojian.washtheclothes ());        Console.readkey ();    }}
Look at the results.

Next, I hired a nanny for my family.

[Code]csharpcode:
Using System;using system.collections.generic;public Delegate string Nanny (); This is in the talent market register, tell him I want to hire a babysitter, I can see the following I do not change the work class xiaojian{public    string washthedishes ()    {        return "washing dishes ... ";    }    public string Cleanthefloor ()    {        Return "sweeping the floor ... ";    }    public string washtheclothes ()    {        return "laundry laundry ... ";    }}
[Code]csharpcode:
Using system;using system.collections.generic;class dohousework{    static void Main (string[] args)    {        Xiaojian Xiaojian = new Xiaojian ();        Nanny Nanny = new Nanny (xiaojian.washthedishes); Here we find a nanny, immediately arranged for her to wash the dishes of the work        Console.WriteLine (Nanny ());        Nanny + = Xiaojian.cleanthefloor; This is to tell her to sweep the ground after washing the dishes        Console.WriteLine (Nanny ());        Nanny + = xiaojian.washtheclothes; This is to tell her to wash the clothes after the sweep        Console.WriteLine (Nanny ());        Console.readkey ();    }}

Look at the results.

Sharp-eyed children's shoes found, since the nanny, I still stay at home, then I will not be new out of the next * * * to the redundant Xiaojian Xiaojian = new Xiaojian (); Remove * * *

[Code]csharpcode:
Using System;using system.collections.generic;public Delegate string Nanny (); class Xiaojian//Has there been any change in this class? Yes, all the methods have become static methods {public    static string washthedishes ()    {        return "washing dishes ... ";    }    public static string Cleanthefloor ()    {        return "sweep sweeping ... ";    }    public static string Washtheclothes ()    {        return "laundry laundry ... ";    }}
[Code]csharpcode:
Using system;using system.collections.generic;class dohousework//go back to doing housework this class, I have no more new out! {    static void Main (string[] args)    {        Nanny Nanny = new Nanny (xiaojian.washthedishes);        Console.WriteLine (Nanny ());        Nanny + = Xiaojian.cleanthefloor;        Console.WriteLine (Nanny ());        Nanny + = xiaojian.washtheclothes;        Console.WriteLine (Nanny ());        Console.readkey ();    }}

It seems that this case, still do not know what to do with the Commission? Well, here's a summary for you:

1. The Commission is the original to you do is to ask someone to do it, for example, Xiaojian.washthedishes () ==>nanny + = xiaojian.washthedishes; you just tell her what to do (for example, Nanny + = Xiaojian.cleanthefloor;), arrange it later and say, go to work (Nanny ())!

2. Take a look at how the delegates are going to use

(1) First in the talent market register, tell him I want to hire a nanny, for example, public delegate string Nanny ();

(2) then hire a babysitter when needed, for example, Nanny Nanny = new Nanny (xiaojian.washthedishes); Remember to arrange a job for her, and if she's free to hire her?

(3) If there's anything else she needs to do, just use + = to tell her, for example, nanny + = Xiaojian.cleanthefloor; Likewise, there's one thing you don't have to do with-=, for example, nanny-= Xiaojian.cleanthefloor;

(4) When things are arranged, tell her to go to work. Go Go go! For example, Nanny ();

Is that all right? Do you understand me?

"Turn" C # Delegation of Notes (Delegate) (i)

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.