A preliminary study on new features of C # 3.0 Part3: Using lambda expressions

Source: Internet
Author: User

Lambda expression (Lambda Expression)
It can be considered an anonymous method of implementation.

With the introduction of the concept of anonymous methods in C # 2.0, we can write the following code:
1class Program
2{
3static void Testlambdaexpression ()
4{
5 list<int> List = new list<int> ();
6
7 list. ADD (1);
8 list. ADD (2);
9 list. ADD (3);
Ten list. ADD (4);
11
List<int> evennumbers = List. FindAll (delegate (int i) {return (i% 2) = = 0;});
13
foreach (int evennumber in evennumbers)
15 {
Console.WriteLine (Evennumber);
17}
18}
19}
20
21static void Main (string[] args)
22{
23TestLambdaExpression ();
24}
25
Anonymous methods to avoid some "minimal function" of the emergence of appropriate use can improve the readability of the code, appears to be a little more refreshing.
(The FindAll method of the list requires a delegate parameter, so we can Sheha)

In C # 3.0, we can write this:


1static void Testlambdaexpression ()
2{
3var list = new list<int> ();
4
5 list. ADD (1);
6 list. ADD (2);
7 list. ADD (3);
8 list. ADD (4);
9
10var evennumbers = list. FindAll (i => (i% 2) = = 0);
11
12foreach (int evennumber in evennumbers)
13{
Console.WriteLine (Evennumber);
15}
16}
where I=> (i%2) ==0 is what we call Lambda Expression, where it plays the role of the original anonymous method, while increasing the readability of the code.
The following is an example of a more complex lambda expression:
1fruit. Filterby ((string name, double price) => name = = "Banana" && price < 2.00);
See this example should be able to write some things yourself (downstairs to buy a Coke to go also)

Here's another interesting thing: Lambda Expression tree (lambda expression trees)
This is a technique for using expressions as data at run time, we can control and change expressions flexibly at runtime,
enhance the flexibility of the program!
or look at the code, so straightforward:
 1using system.expressions
 2
 3static void Main (string[] args)
 4{
 5     expression< Func<int, bool>> filter = n => (n * 3) < 5;
 6
 7     binaryexpression lt = (binaryexpression) filter. Body;
 8     binaryexpression mult = (binaryexpression) Lt. left;           
 9     ParameterExpression en = (parameterexpression) mult. left;           
10     ConstantExpression three = (constantexpression) mult. right;
11     ConstantExpression five = (constantexpression) Lt. right;
12                       
13     Console.WriteLine ({0} ({1} {2} {3}) {4}), Lt. NodeType,
14              mult. NodeType, en. Name, three. Value, five. Value);
15}
Output:
(LT (Multiply n 3) 5)
With this technique, we can make it easier to manipulate data and data expressions, not just to write a method.







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.