Analysis of Delegate in C #

Source: Internet
Author: User
Tags define function 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. Horizontal comparison with C++,delphi

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 you define function pointers, and the functions they can point to define incoming arguments that must be solid and outgoing as integers.

3. What's the use of managed functions

Because a managed function is a reference to a function within a class. So we don't have to know what the specific name of the function is, but we just need to 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 (obtain market information, produce what product), only need to tell the secretary to send me a market report or we do not produce an atom bomb to turn to produce a hydrogen bomb. Then the secretary according to the command of the leadership of the choice to find which employee, and then the information returned to the leadership of the staff. The three employees are transparent to the leader, and the leader does not know who is executing the order.

4. How to define and 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;}
}
  
OK, so how do you do it? How does the boss use the Secretory class?
  
Go on
  
The boss only need 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 above two classes of methods, one is static, one is not static. The functions that a managed function refers to can be static or static.

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.