C # delegate is unable to pass the delegate

Source: Internet
Author: User
Tags reflector

In the previous blog, we introduced the logic control program of an automated system in the data center. Lambda expressions are used to facilitate the implementation of program functions. however, we should not only strive to implement functions and complete tasks. We should know what it means, that is, knowing Lambda expressions can bring such convenience, we should also know why it brings such convenience. Next, let's take a small journey with you.
 
To find out why, we use Reflector to decompile the source program,
 
?
Static void Main (string [] args)
{
Monitor. OpenEventHandler OnOpen; // defines a delegate variable
 
OpenSomething openSomething = new OpenSomething (); // instantiate an object for enabling
 
OnOpen = openSomething. OpenPower;
 
OnOpen + = openSomething. OpenServer;
 
OnOpen + = () => openSomething. OpenServices (5); // the reference of the method is still assigned to the delegate variable.
 
OnOpen ();
 
Console. ReadKey ();
}
}
  
 
Use Reflector to decompile and view the Main method of the Program class, as shown in
 
 
First, instantiate an OpenSomething object, then instantiate the OpenEventHandler-type delegate variable, and assign the reference of the OpenPower method to it, which is completed in two lines in the source program, this is because of the decompilation, and the truth is the same.
The next line of code reveals the principle of assigning values to delegates, that is, the "+ =" symbol we use. After decompiling, we can see that, it actually calls the Combine method of the Delegate class. The function of this method is to connect two delegates. It has two parameters, the parameter type is Delegate, and the first one is OnOpen, that is, the original delegate. The second is another delegate that needs to be connected, new Monitor. openEventHandler (openSomething. openPower) is actually a new delegate variable instantiated and connected to OnOpen. Therefore, we know that when we use "+ =" in daily use, it seems that we splice the method name "to the end of a delegate variable. When we call a delegate, these methods are called in sequence, in fact, it is to Combine two delegate variables of the same type ......
 
The following line of code corresponds to the Lambda expression in the source program. Different from the previous line, the second parameter uses delegate {...} Syntax: This is an anonymous delegate. It can be seen that when Lambda expressions are used, the internal implementation principle is actually a new anonymous delegate that can pass parameters. In this way, we can understand that, when the connection type is the same as the delegate variable, the program will instantiate a new delegate variable, and connect different types of delegate variables, the program will define and instantiate an anonymous delegate for connection, for the connection between delegate variables, they are not required to have the same delegate type.
Lambda expressions encapsulate previous technologies to a certain extent. As for the reason for encapsulation and many other features besides encapsulation, it is worth studying. When learning a new technology for ourselves, we need to develop a habit of knowing what it is, knowing that Lambda expressions can be used in this way, and then going through the inquiry similar to this article, is it more impressive. After learning about it, you can use it to make it easy, and make it more controllable. If something goes wrong, you can easily find it. This is like in a company, if the boss wants to assign an important responsibility to his employees, he must first understand the character of the employee and consider whether he is suitable for doing so, not just looking at his abilities.

 


From white light

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.