What is a delegate in C #?

Source: Internet
Author: User

Overview

Delegates are similar to function pointers in C + +, but they are different. In C + +, a function pointer is not type-safe, it points to a location in memory, and we cannot tell what the pointer actually points to and is more difficult to know about the parameters and the return type. The. NET delegate is completely different, it's a type-safe class, and we can clearly know the return type and parameter type of the delegate definition. It can include not only a reference to a method, but also a reference to more than one method. This is the delegate, the. Addressing method. NET version.

Understanding delegates

In the actual development process, sometimes the operation of a method is not for the data, but for another method, but this method is not known at compile time, only at run time to know. If it is for the operation of the data, just pass the data as a method parameters, then the method to do the operation, it is obvious that the method as a parameter, it sounds strange, but need to accept the reality, and then from the use to understand its meaning. Here's an example (please forgive my English, my 中文版 is poor! ), such a scene: The Matchmaker to the mushroom cool or young man introduced objects, if the object is male, mushroom cool to say her contact way, if it is female, the boy said his contact way (PS: We are here are normal sexual orientation).

Take a look at the code map

In the namespace, define a delegate, and the class is peer

Public delegate string Gettelphone (); Used to get the contact information.

As mentioned in the overview, a delegate can define a return type and a parameter type, then the return type of the delegate is string, without parameters.

Go on....

Matchmaker class:

    <summary>//Matchmaker///    </summary>    class Meipo    {public        string Sex {get; set;}        public void Gettel (Gettelphone gettelphone)        {            Console.WriteLine (Gettelphone.invoke ());        }    }

As you can see, the parameters of the Gettel method are of a delegate type, and are used just like the general base type and the common reference type reference. Then the method body executes the delegate method and outputs the result.

Mushroom Cool Type:

    <summary>    //mushroom cool///</summary> class Girl {public        string Getgirltel ()        {            return "get mushroom cold phone";        }    }

Small Gang Category:

    /// <summary>    /// male cock Silk     /// </summary>    class Diaosi    {        publicstring  Getboytel ()        {            return"  get a male cock on the phone ";        }    }

The last Test class

        Static voidMain (string[] args) {            varRedmun =NewMeipo {Sex ="Male"}; varDel=NewGettelphone (NewGirl ().            Getgirltel); if(redmun.sex=="female") del=NewGettelphone (NewDiaosi ().            Getboytel);            Redmun.gettel (DEL);        Console.readkey (); }

To perform the steps:

1, create matchmaker object, Matchmaker introduced a sex= "male",

2, create the delegate object, the delegate is a class, so with new to instantiate. A delegate refers to a reference to a method, where the method that is passed in when it is initialized references new Girl (). Getgriltel

3. If the matchmaker introduces the sex= "female", then the delegate points to another method

4, Matchmaker object call Gettel get contact information

Summary: The content of the delegation is more complex, in view of the author's limited level, understanding is limited, here first explain so much, later if understanding deeper, continue to share.

What is a delegate in C #?

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.