Delegate in C #

Source: Internet
Author: User

1. What is a managed function?

A managed function is a reference to a function in a class. It does not have a specific function definition, but only points to a function implementation.

2. What is the use of managed functions?
Managed functions are a reference to a function in the class. therefore, we do not need to know the specific name of this function. Instead, we only need to call the managed function so that the managed function can call the corresponding function. example: A company, a leader, a leader, and three employees manage the market, plan, and produce respectively. the secretary here is equivalent to a hosting function. What commands should the leader give (obtain market information and produce what products ), you just need to give me a market report to the secretary or we don't need to generate an atomic bomb to generate a hydrogen bomb. then, the secretary selects the employee based on the command of the lead and returns the employee's information to the lead. the three employees here are transparent to the leaders and the leaders do not know who executes the commands they have issued.

3. How to define and use

In the preceding example, we declare a delegate-managed function prototype.
Class secretory
{
// Define the managed function prototype
Private delegate string do (string command );
// Create a managed function instance
Public do doit;
// Issue a command to the Secretary for execution

Public void executecommand (string command)
{
Switch (the Secretary determines who to execute according to the Command)
{
Case market research personnel:
Doit = new do (New marketman (). getmarketinfo );
Break;
Person who produces the case product:
Doit = new do (New productman (). produceproduct );
Break;
Default:
Break;
}
}
Doit (command );
}

/// Category of the person who investigates the market
Class marketman
{
Public String getmarketinfo (string command)
{According to the command investigation information, return the obtained information ;}
}

}

/// Producer
Class productman
{
Public static string produceproduct (string command)
{

Generate the product according to the command and return the information in the production project ;}
}

}
So how to execute it? How does the boss use the secretory class?
Go on
The boss just needs to call the Secretary (new) and issue a command directly to him. Then he can drink tea ~ Pai_^

Class boss
{
Public void static main ()
{
(New secretory (). doit ("person who produces the product ");
}
}

4. Miscellaneous

As you can see, one of the methods in the above two classes is static, and the other is not static. The functions referred to by managed functions can be static or not 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.