Analysis of Delegate in C #

Source: Internet
Author: User
Tags define function definition function prototype
1. What is a managed function

A managed function is a reference to a function within a class. It does not have a specific function definition, it simply points to a function implementation.



2. With C + +, Delphi horizontal Comparison

The types that correspond to managed functions in C + + and Delphi are function pointers in the form of the following:

C++

typedef (int getnum) (real rnum);

Delphi

function Getnum (rnum:real): integer; External

Here defines the function pointers, the functions they can point to define the incoming arguments must be solid, outgoing for the Shaping



3. What's the use of managed functions

Because a managed function is a reference to a function within a class, we do not need to know what the name of the function is, but just call the managed function and let the managed function call the corresponding function. An example: A company, a leader, a leading secretary, three employees manage the market separately, planning and production. Here the Secretary is quite with a managed function, the leader wants to give what order (get market information, produce what product), Just tell the secretary to give me a market report or we do not produce an atom bomb to turn to the production of a hydrogen bomb. Then the secretary according to the command of the choice of the staff to find which employees, and then the information returned to the leadership staff. The three employees are transparent to the leader, and the leader does not know who is executing the order.



4. How to define && how to use



And then the example above, we declare a secretary managed function prototype

Class secretory

{

Defining a Managed function prototype

Private delegate string do (String command);

Creating a Managed Function instance

public do Doit;

Give the secretary an order to execute

public void ExecuteCommand (String command)

{

Switch (the secretary according to the order to judge who is to be executed)

{

Case Research Market Person:

Doit=new Do (new Marketman (). Getmarketinfo);

break;

Case-growing product person:

Doit=new Do (new Productman (). PRODUCEPRODUCT);

break;

Default:

break;

}

}

Doit (command);

}



The type of person who studies the market

Class Marketman

{

public string Getmarketinfo (String command)

{According to the command survey information, return to get information;}

}

The person who produces the product

Class Productman

{

public static string produceproduct (String command)

{Produce the product according to the order and return the information in the production engineering;}

}





How to do it:??????? How does the boss use the Secretory class???

The boss just has to call the Secretary (new), directly to him to give orders, and then you can drink tea ~

Class Boss

{

public void static Main ()

{

(New secretory ()). Doit ("We're going to produce a hydrogen bomb and throw all the previous bombs into Japan");

}

}



5. Other

As you can see, the methods in the two classes above are static and one is not static. The functions that a managed function refers to can be static or not static.

6. You are welcome to add and shoot bricks ~



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.