C # Lambda expression learning notes

Source: Internet
Author: User
    Here we will introduce the C # Lambda expression, which is actually no different from the anonymous method. Lambda's input parameters correspond to the parameters in the delegate brackets. Since C # Lambda expressions can infer the parameter type, the parameters here do not need to be declared.

     

    C # is a common language. Here we mainly introduce C # Lambda expressions, including a Lambda expression as a delegate and a delegate pointing to a method.

     

    Have you ever been familiar with C # Lambda expressions? Do you think the anonymous method is very good and it reduces a lot of code? But the use of anonymous methods is not human-oriented. What is human-oriented? For example, you can read the program code in a natural language. In. NET 2.0, the system. Collections. Generic namespace contains some new methods in the list. For example, find. How can we call an anonymous method:

     

 
 
  1. books.Find(delegate(Book book){return book.Price < 50;}); 

The code is very simple, but it cannot be read out. Let's take a look at the C # Lambda expression:

 

Books. Find (Book => book. Price <50); this C # Lambda expression can be read as follows: to give you a book, if its price is less than 50, return true.

 

Well, let's go into the C # Lambda expression:

 

 

After the C # Lambda expression assembly is decompiled, we find that it is actually no different from the anonymous method. Lambda's input parameters correspond to the parameters in the delegate brackets. Since C # Lambda expressions can infer the parameter type, the parameters here do not need to be declared.

 

Lambda operators are read as "goes to", followed by expressions or statement blocks (this is also different from anonymous methods. Anonymous methods can only use statement blocks but not expressions ), the following example shows the types of C # Lambda expressions:

 

 
 
  1. // The type of X is omitted. the compiler can deduce it based on the context, followed by an expression.
  2. X =>X + 1
  3. Deleage (int x) {return x + 1 ;}
  4. // The block is followed by the statement Block
  5. X =>{Return x + 1 ;}
  6. Delegate (int x) {return x + 1 ;}
  7. // The input parameter can also contain the type. Do not forget the parentheses after the type.
  8. (Int x) =>X + 1
  9. Delegate (int x) {return x + 1 ;}
  10. // You can also enter multiple parameters separated by commas (,). Do not forget parentheses.
  11. (X, y) =>X + Y
  12. Delegate (int x, int y) {return X + Y ;}
  13. // No parameters are returned.
  14.  
  15. () =>1
  16.  
  17. Delegate () {return 1 ;}

This is the way C # Lambda expressions are used, but there are many stories and mysteries behind lambda. The C # Lambda expression can be used to construct an Expression Tree, and the expression tree is as important as the root of the tree for the LINQ clause. We will not discuss the expression tree here. This is not something that can be clearly stated in a few words. We will discuss it further when the time is ripe.

 

C # Read more about lambda expressions

 

Lambda actually has a long history. The machines we use today are from the Von noriman system and belong to the Turing Machine. Before that, there was another theory called the lambda algorithm, but the Turing machine was first implemented, as a result, the lambda algorithm became a functional programming language, especially lisp. In functional programming languages, functions were the first element, function parameters, and function return values were all functions, the program has no variables and functions are nested. Functional programming languages have always existed in the ivory tower, so they are not widely used in the industry. However, in recent years, the industry prefers the "Retro" style, therefore, functional programming languages are slowly taking the stage of history. Functional programming can solve some imperative Programming Problems (or it is very troublesome to solve ). C # What should I do with function-style programming? The method defined by the original method is definitely not feasible. 2.0 of anonymous methods solve this problem in a program, but it is still not enough, lambda in 3.0 has finally been well solved. A Lambda is a delegate, and a delegate points to a method. Now we can simply pass the method as a parameter using lambda, layers can also be nested, which is very simple.

 

 

 

 

 

 

 

 

 

 

 

 

 

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.