The veil of Lambda expressions (1)

Source: Internet
Author: User
Tags reflector

In. NET3.0, Microsoft released a Lambda expression. This makes the expression of the Code more elegant. However, most of the new things will be rejected by instinct. Although version 3.0 has been launched for a long time, I have learned from people around me that there are not many Lambda users, the old method is used to replace it even if it is usable. Actually, Lambda expressions are not mysterious. Let's unveil C # Lambda together.

The following simple Program contains a Lambda expression, class Program {delegate string MyDelagate (string val1, string val2); static void Main (string [] args) {MyDelagate my = (param1, param2) =>{ string test = "internal variable"; return param1 + param2 + test ;}; Console. writeLine (my ("parameter 1", "parameter 2"); Console. read () ;}}: of course, this small example does not reflect the advantages of Lambda. The benefits of Lambda will be introduced in the next chapter. Let's first look at the essence of Lambda expressions, which is to be seen through phenomena. Now we need a decompilation tool. net reflector (download URL: http://115.com/lb/5lbd3lgqkupj) Open the software, will compile the project file with reflector open decompilation results and we write basically consistent, because we chose 4.0, lambda expressions launched by Microsoft at 3.0. Let's Select. NET2.0 and then decompile it to see what it will look like. Familiar? Isn't that the way we are familiar with anonymous methods. Since Lambda expressions are upgraded to anonymous method writing, what will happen when the. net platform does not support anonymous methods. Let's restore the program: This is a delegate that calls a method, and the method returns a string concatenated by three variables. This program is consistent with the functions implemented by the previous Lambda expressions. Class Class2 {private delegate string MyDelagate (string val1, string val2); static void Main (string [] args) {MyDelagate my = new MyDelagate (Method); Console. writeLine (my ("parameter 1", "parameter 2"); Console. read (); return;} private static string Method (string param1, string param2) {string test = "internal variable"; return (param1 + param2 + test );}} through the above analysis, we have unveiled Lambda and also unveiled its development history. 1. in. in net1.0, functions can only be implemented through method calls in the program. or call the method by defining the delegate. 3. in. net2.0 supports anonymous delegation, so you can write anonymous methods to implement functions. 4. A Lambda expression was introduced in. net3.0, which allowed us to beautify the code structure while implementing functions.

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.