[. Net 3.x new feature 3] lambda expressions

Source: Internet
Author: User
With the passage of time, vs gradually enters 2005 from 2008,. NET will also enter 3.5, and the pace of learning will gradually catch up. Today, let's take a look at another special Lambda expression of C #3.0, which evolved from the 2.0 proxy and is also necessary for preparing for LINQ. Lambda expression first needs to know its operator "=>". In my previous articleArticleYou can get an example of using proxies in different versions and using lambda expressions. You can see here: [C #3.0] overview of new features . Let's take another example today to better understand this Lambda expression, because we often use this stuff in LINQ, so we should take a good look, besides, this is not difficult.

In[. Net 3.x new feature 1] automatic attributes, object initialization and set InitializationDescribes the initialization of automatic attributes and sets. To illustrate lambda expressions, we first define an object class,CodeAs follows:

1 Public   Class Person
2 {
3 Public   String Name { Get ; Set ;}
4 Public   String Nickname { Get ; Set ;}
5 Public   Int Age { Get ; Set ;}
6 }

We initialize a list <person> list and obtain the average value of age in the person instance with age 23 and all instances in the class table. Of course, the extension method is also used, which belongs to the scope of LINQ. The Code is as follows:

1 List < Person > People =   New List < Person >
2 {
3 New Person {name = " Xiaobing " , Nickname = " Cyber soul soldier " , Age = 23 },
4 New Person {name = " Qingqing District " , Nickname = " Qqing " , Age = 22 }
5 };
6 // Obtains the person instance whose age is 23 in people.
7 Ienumerable < Person > Results = People. Where (P => P. Age =   23 );
8 // Calculate the average age in people.
9 Int Perage = People. Average (P => P. Age );

Have you seen the lambda expression? P => P is a Lambda expression. Of course, where and average are extension methods and are an extension method of LINQ. Of course we can do this in C # or. NET 2.0, but it is not that simple (it is not difficult). Let's take a look at the implementation method in. NET 2.0:

1 List < Person > People =   New List < Person >
2 {
3 New Person {name = " Xiaobing " , Nickname = " Cyber soul soldier " , Age = 23 },
4 New Person {name = " Qingqing District " , Nickname = " Qqing " , Age = 22 }
5 };
6
7 Ienumerable < Person > Results = People. Where ( Delegate (Person p ){ Return P. Age =   23 ;});
8
9 Int Perage = People. sum ( Delegate (Person p ){ Return P. Age ;});

We can achieve the same effect from above, but the code is a little more complicated than using lambda. Let's take a look at how the extension of this proxy method is implemented? First, right-click and select "go to definition" to see the where extension:

1 Public   Static Ienumerable < Tsource > Where < Tsource > (
2 This Ienumerable < Tsource > Source, func < Tsource, Bool > Predicate );
3 Public   Static Ienumerable < Tsource > Where < Tsource > (
4 This Ienumerable < Tsource > Source, func < Tsource, Int , Bool > Predicate );

Let's take a look at the where code:

1 Public   Static Ienumerable < Tsource > Where < Tsource > ( This Ienumerable < Tsource > Source,
2 Func < Tsource, Bool > Predicate)
3 {
4 Foreach (Tsource s In Source)
5 {
6 If (Predicate (s ))
7 {
8 Yield Return S;
9 }
10 }
11 }

From the code above, we can see that it is an extension of ienumerable <t>, while predicate is a func <t, bool> proxy, and the second type of func proxy is the returned type, the first type is the parameter type. If it is func <t, Int, bool>, the last one is the type returned by the proxy (that is, the result after the proxy method is executed), and the others are parameters (parameters of the proxy method, in the preceding Code, if the predicate statement returns true after execution, iteration returns instance S.

OK today, this expression is often used in LINQ, so that he can go deep into it in our application and practice.

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.