What is the delegate in C ?,

Source: Internet
Author: User

What is the delegate in C ?,

Overview

The delegate is similar to the function pointer in C ++, but it is different. In C ++, the function pointer is not of type security and points to a location in the memory. We cannot determine what the pointer actually points, it is more difficult to know the parameters and return types. The. NET delegate is completely different. It is a type-safe class, and we can clearly understand the return type and parameter type defined by the delegate. It can not only contain reference of methods, but also contain references of multiple methods. This is the. NET version of the Delegate. Addressing method.

Understanding Delegation

In the actual development process, sometimes the operations performed by a method are not for data, but for another method. However, this method cannot be known during compilation, it can only be known at runtime. If you want to operate on data, you only need to pass in the data as a parameter of the method. If you want to operate on the method, it is obvious that the method can be passed in as a parameter. It sounds really strange, but we need to accept this reality first, and then understand its meaning in use. The following is an example (forgive me for my english, my english is poor !), In this scenario, the matchmaker will introduce the target person to Yi Liang or the young guy. If the target person is a male, Ma Liang will give her contact information. If the target person is a female, the young man will give his contact information (PS: we all have a normal sexual orientation here ).

View the code Diagram

 

In the namespace, define a delegate, which is at the same level as the class.

Public delegate string GetTelphone (); // used to obtain the contact information.

As mentioned in the overview, a delegate can define the return type and parameter type, so the return type of this delegate is string, with no parameters.

Continue ....

 

Matchmaker category:

/// <Summary> // matchmaker // </summary> class MeiPo {public string Sex {get; set;} public void GetTel (GetTelphone getTelphone) {Console. writeLine (getTelphone. invoke ());}}

As you can see, the parameter of the GetTel method is a delegate type, which is used the same as the general basic type and common reference type reference method. Then, the method body executes the delegate method and outputs the result.

 

Cooling category:

/// <Summary >/// binliang /// </summary> class Girl {public string GetGirlTel () {return "";}}

GUY:

/// <Summary> // male DiaoSi // </summary> class DiaoSi {public string GetBoyTel () {return "get male phone number ";}}

Last test class

Static void Main (string [] args) {var redMun = new MeiPo {Sex = "male"}; var del = new GetTelphone (new Girl (). getGirlTel); if (redMun. sex = "female") del = new GetTelphone (new DiaoSi (). getBoyTel); redMun. getTel (del); Console. readKey ();}

Procedure:

1. Create a matchmaker object. The matchmaker introduces a sex = "male ",

2. Create a delegate object. The delegate is a class, so it is instantiated using new. The delegate refers to the reference of the method. The reference of the method that is passed in during initialization is new Girl (). GetGrilTel

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

4. The matchmaker calls GetTel to obtain the contact information.

 

Summary: The delegated content is complicated. In view of the limited level and limited understanding of the author, I would like to explain so much here. If so, I will continue to share it later.

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.