The third part of the path to Lambda masters

Source: Internet
Author: User


Through the famous linqpad, we can have a deeper look at msilCodeThere is no secret. Is the use of a linqpad

Let's take a look at three examples. The first Lambda expression is as follows:

Action <String> Dosomethinglambda = (S) =>{Console. writeline (s );//+ Local};


The corresponding common functions are as follows:

Action <String> Dosomethinglambda = (S) =>{Console. writeline (s );//+ Local};


The generated msil code snippet is as follows:

Dosomethingnormal: il_0000: NOP il_0001: ldarg.1Il_0002: Call system. Console. writelineil_0007: NOP il_0008: Ret<Main>B _ 0: il_0000: NOP il_0001: ldarg.0Il_0002: Call system. Console. writelineil_0007: NOP il_0008: Ret


The biggest difference is that the method name usage is different. Instead of declaration. In fact. The statement is exactly the same. The compiler creates a new method in the class to implement this method. This is nothing new, just to facilitate code writing by using lambda expressions. From msil code, we did the same thing. A method is called in the current object.

Let's demonstrate the changes made by the compiler. In this example. We can see that the compiler moves the lambda expression into a fixed method.

The second example shows the real wonders of lambda expressions in this example. We use both common methods with global variables and lambda expressions with captured variables. The Code is as follows:

 Void Main (){  Int Local = 5  ; Action < String > Dosomethinglambda = (S) => {Console. writeline (S + Local );};  Global = Local; dosomethinglambda (  "  Test 1  "  ); Dosomethingnormal (  " Test 2  "  );}  Int   Global  ;  Void Dosomethingnormal ( String  S) {console. writeline (s) + Global  );} 


It seems that there is no difference. The key is: how lambda expressions are processed by the compiler

Il_0000: newobj userquery + <> C _ displayclass1.. ctoril_0005: stloc.  1 Il_0006: NOP il_0007: ldloc.  1  Il_0008: LDC. i4.  5  Il_0009: stfld userquery + <> C _ displayclass1.localil _ 000e: ldloc.  1  Il_000f: ldftn userquery + <> C _ displayclass1. <main> B _ 0il_0015: newobj system. Action <System. String> .. Ctoril_001a: stloc.  0  Il_001b: ldarg. 0  Il_001c: ldloc.  1  Il_001d: ld1_userquery + <> C _ displayclass1.localil _ 0022: st1_userquery.  Global  Il_0027: ldloc.  0  Il_0028: ldstr  "  Test 1  "  Il_002d: callvirt system. Action <System. String>. Invokeil_0032: NOP il_0033: ldarg.  0  Il_0034: ldstr  "  Test 2  "  Il_0039: Call userquery. dosomethingnormalil_003e: NOP dosomethingnormal: il_0000: NOP il_0001: ldarg.  1  Il_0002: ldarg.  0  Il_0003: ld1_userquery.  Global Il_0008: box system. int32il_000d: Call system. String. concatil_0012: Call system. Console. writelineil_0017: NOP il_0018: Ret <> C _ displayclass1. <main> B _ 0: il_0000: NOP il_0001: ldarg.  1  Il_0002: ldarg.  0  Il_0003: ld1_userquery + <> C _ displayclass1.localil _ 0008: box system. int32il_000d: Call system. String. concatil_0012: Call system. Console. writelineil_0017: NOP il_0018: Ret <> C _ displayclass1.. ctor: il_0000: ldarg. 0  Il_0001: Call system. Object .. ctoril_0006: Ret 


Same as the first example. Same mechanism. The compiler moves the lambda expression to a method. But the difference is that the compiler also generates a class this time. The methods generated by the compiler for Our lambda expressions are placed in the class, which gives the captured variables a global scope. Lambda expressions can access local variables. Because it is in msil. It is a global variable in a class instance.

Therefore, all variables can be assigned/read in the newly generated class object. This solves the issue of referencing between variables. (In fact, only the reference to this type of instance is retained .) The compiler is smart enough to put the captured variables into the class. Therefore, Lambda does not have much performance problems. In any case. Note. As lambda expressions are referenced, memory leakage may occur. As long as the method is still in progress. The variable remains alive. Obviously. Now we know why.

We use the illustration again. In this case. Not only will methods be moved. The captured variables are also moved. All moved objects will be placed in a new class. Therefore, a class without a name appears implicitly.

The next section will be the ing of the popular javascrpit mode.

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: 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.