. NET Delegation

Source: Internet
Author: User

People are familiar with linked lists, and trust most of them by delegation. NET programmers are also familiar with it. These two technologies are often used during normal development, but linked lists are in. NET has been encapsulated, making it more convenient to use the Collection type. In some cases, it is necessary for us to understand how to encapsulate these convenient and fast, delegation is a special type in. NET is used as an encapsulation of methods. In some unmanaged code, C ++ is not so lucky, we have to deal with complex address transformations, pointer maintenance for linked lists, and address Retrieval for memory. Microsoft has always been an object of human imitation, whether it is his operating system or development platform IDE, It is very comfortable. Some people may say that programmers should become popular in such a lofty and mysterious profession. People will write programs; this problem is a matter of benevolence and wisdom;

What I want to talk about today is. the delegated chain in. NET may have very few technical terms, but the concept can be understood literally, that is, the relationship between the delegated chain and the linked list; simply put, a delegate saves it through a linked list and calls it in sequence. For information about delegation and events, refer to my "delegation and events" article. A delegate is a pointer to a method, methods are encapsulated as objects for convenient, secure, and asynchronous calls. In asynchronous mode, CLR processes them through background threads. We do not need to worry about thread scheduling mutex issues, in special cases, we also need to encapsulate it from the beginning. A delegate is a type that encapsulates the method to be called by instantiating multiple delegate instances, each method is pushed into the data structure of the stack during the program running. before calling the method, you must determine the method address. All methods are independent of each other through the reference type for convenient searching and calling; when multiple delegated instances are together, a data structure such as the delegated chain is formed. For example, a Click event in the Button that we often use will be passed through:

Button. click + = New delegate type (method 1) Add a delegate instance to the delegate linked list. I will continue to add a method to the linked list to go to the Button. click + = New delegate type (method 2). At this time, two delegate instances are in the delegate linked list, that is, two methods are called. The linked list is ordered, method 2 is followed by method 1. The Code call sequence is to call method 1 first, and then add the processing result to method 2, in this way, the overall call to the delegate chain is formed. When using the service, some details need to be noted that it is also a performance problem. "code is always the culprit that harms program performance." In the above Code, Btton. click + = New delegate type (method 1), which can be written as Button. click + = method 1, which reduces the memory allocation of a delegated instance. The system uses the sequence table to save the calling method, instead of the linked list, anyone who wants to learn the performance differences between linked lists and sequence tables knows the <Data Structure>; if you do not need to use a method in the linked list, you can use the delegate removal expression to remove the Button from the delegate linked list. click-= method 1. I have removed method 1 from the delegate linked list. The Code will not go to method 1 in the next call, we often encounter this situation during the development process: I want to call N methods when an event occurs, but I cannot control the execution sequence, at this time, we can remove or add methods from the linked list to easily control the order of the methods in the delegate chain. What is this situation, I have ten methods to process the program logic, but these 10 methods are not dead. I need to dynamically jump from these 10 methods to other logic processing based on different conditions and then return to this method, the program will never break away from these ten methods. At this time, we need to use the delegate parameters. At this time, we need to define a type for passing in the delegate linked list, method: determine the status of this type of instance and determine whether to continue to pass down;

Figure 1:

There are four methods in this delegate chain. When I execute method 2, I need to judge whether the user's input is correct and whether to let the subsequent methods continue to execute:

Public class Class1
{
/// <Summary>
/// Delegate type
/// </Summary>
/// <Param name = "ismove"> indicates whether to execute downward </param>
Public delegate void Print (ref string ismove );
/// <Summary>
/// Delegated instance
/// </Summary>
Print p;
/// <Summary>
/// Default constructor
/// </Summary>
Public Class1 ()
{
// Add four methods
Print p = new Print (method1); // The first method is used to instantiate the delegate to the first instance.
P + = method2;
P + = method3;
P + = method4;
}
/// <Summary>
/// Start the execution of the delegate chain
/// </Summary>
/// <Returns> </returns>
Public string run ()
{
String ismove = "yes ";
P (ref ismove );
Return ismove;
}
Public void method1 (ref string ismove)
{
If (ismove = "yes") // you can determine whether or not to continue the next step.
{

}
}
Public void method2 (ref string ismove)
{
If (ismove = "yes") // you can determine whether or not to continue the next step.
{
// An error occurred while executing the command. I don't want to execute the command again.
Ismove = "no"; // none of the subsequent methods will be executed
}
}

Public void method3 (ref string ismove)
{
If (ismove = "yes") // you can determine whether or not to continue the next step.
{

}
}
Public void method4 (ref string ismove)
{
If (ismove = "yes") // you can determine whether or not to continue the next step.
{

}
}

We cannot determine which method to remove, so we need to determine that each method call is correct, the method will be called normally, but we have added the judgment to control the execution status;

Figure 2:

In Figure 2, I remove Methods 3 and 4 from the delegate chain, and the subsequent calls will not be called to them;

Public class Class1
{
/// <Summary>
/// Delegate type
/// </Summary>
/// <Param name = "ismove"> indicates whether to execute downward </param>
Public delegate

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.