C #2.0 anonymous method 3

Source: Internet
Author: User
Local variable usage of anonymous methods

So far, we have a basic understanding of how the anonymous method works and how it is implemented internally. Basically, C # creates a private method to encapsulate anonymous methods. At the same time, the signatures of these methods match the delegates they are allocated. Now, let's take a look at the followingCode:

Public class Program
{
Public Delegate void mydelegate ();
Public static void main (string [] ARGs)
{
Int itemp = 100;
Mydelegate DLG = delegate
{
Console. writeline (itemp );
};
DLG ();
}
}

For the content we have learned about anonymous methods so far, this code should not be compiled. Because we didn't use instance data members, the C # compiler should create a Private Static Method in the ''program ''class to wrap this anonymous method. But how does the new method access local variables? This makes us believe that the Code cannot be compiled. But surprisingly, the C # compiler successfully compiled the code without any errors or alarms. In addition, when you execute this example, the correct value of the itemp variable is printed on the console screen. Now let's go to the high-level topic of anonymous methods. An anonymous method can encapsulate the values of environment variables used in its method body. This encapsulation applies to all local variables in the method defined by the anonymous method. When the C # compiler identifies a local variable in an anonymous method body, it will do the following:

1. Create a new private class as an internal class of the class defined by the anonymous method.

2. Create a public data member in the new class (: internal class) and use the same type and name as the local variable used in the anonymous method body.

3. Create a public instance method in the new class that encapsulates anonymous methods.

4. Replace the declaration of local variables with the declaration in the new class. Create an instance of the new class to replace the declaration of local variables.

5. Use the data member of the new class instance to replace the local variables used inside and outside the anonymous method body.

6. Replace the definition of the anonymous method with the address of the instance method defined in the new class.

Therefore, during compilation, the above Code will be translated into the following code by the C # Compiler:

Public class Program
{
Private class innerclass
{
Private void instancemethod ()
{
Console. writeline (itemp );
}
Public int itemp;
}
Public Delegate void mydelegate ();
Public static void main (string [] ARGs)
{
Innerclass localobject = new innerclass ();
Localobject. itemp = 100;
Mydelegate DLG = new mydelegate (localobject. instancemethod );
DLG ();
}
}

As shown in the preceding pseudocode, the C # compiler generates a private internal class for the ''projec'' class. The local variables used in the anonymous method are captured as an instance data member of the new internal class. The anonymous method itself is encapsulated in the instance method of the internal class. Finally, the instance method is used as a delegate processor in the main method. In this way, when the delegate is called, a correct value will be provided for the local variable encapsulated into the anonymous method. The selected section in the following figure shows the new private internal class that is silently added to the ''projec'' class by the C # compiler.

Local variables used in anonymous methods have a life cycle that exceeds the conventional external methods that use them. This technology is known as closures in other languages. Besides the simple syntax provided by the anonymous method, closures is a powerful technology provided by the anonymous method to developers. This technology allows delegated processor code (anonymous method) to access the local variables defined within the conventional method. This allows out-of-band data. In addition to the delegate parameters, data will be transferred to the delegate for use during method execution. Without this technology, each delegate and its corresponding processor method have to declare a parameter that represents local context data, over time) it will become difficult to manage.

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.