lambda expression for the. NET 3.x new attribute

Source: Internet
Author: User
Tags bool expression new features

With the passage of time, vs also from 2005 slowly step into 2008. NET will also enter the 3.5 version, the pace of learning to catch up slowly. Today we're going to look at another feature of c#3.0 lambda expression, which evolved from a 2.0 agent and is also a necessary preparation for LINQ. Lambda Expressions First we need to know his operator "=>", in my previous article I can get an example of proxies and lambda expressions used in different versions, which you can see here: [c#3.0] New features Overview. Let's take another example today, so we can better understand this lambda expression, because we often use this in LINQ, so we should look at it, and it's not difficult.

We talked about the initialization of automation properties and collections in the [. NET 3.x new feature one] automatic properties, object initialization and collection initialization, in order to explain lambda expressions we first define an entity class with the following code:

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 it to a list of lists and get the average of age for the person instance of age 23 and all instances in the class table. Of course, the extension method is also used, which is a category of LINQ, the code is as follows:

1    List<Person> people = new List<Person>
2 {
3 new Person{Name="小兵",NickName="网魂小兵",Age=23},
4 new Person{Name="青青",NickName="QQing",Age=22}
5 };
6 //取得people中Age为23的Person实例。
7 IEnumerable<Person> results = people.Where(p => p.Age == 23);
8 //计算people中的平均岁数。
9 int perAge = people.Average(p => p.Age);

See lambda expression? P=>p This is a lambda expression, and of course where and average are extension methods, an extension of LINQ. Of course we can do it in C # or in. NET 2.0, but it's not that simple (but it's not hard), so let's take a look at the implementation methods in. NET 2.0:

1 List<Person> people = new List<Person>
2 {
3 new Person{Name="小兵",NickName="网魂小兵",Age=23},
4 new Person{Name="青青",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 do the same thing from above, but the code is a bit more complicated than using a lambda. Let's take a look at how this extension of the proxy approach is implemented. First of all, we're right. Select Go To definition to see the extension of where:

1 public static IEnumerable<TSource> Where<TSource>(
2 this IEnumerable<TSource> source, Func<TSource, bool> predicate);
3public static IEnumerable<TSource> Where<TSource>(
4 this IEnumerable<TSource> source, Func<TSource, int, bool> predicate);

Let's look at the code for the Where:

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 above code we can see that the extension of the IEnumerable, and predicate is a Func agent, Func the second type of the proxy is the type returned, and the first type is the type of the parameter. If it is Func, the last one is the type returned by the proxy (the result of this proxy method execution), the others are parameters (the arguments of the proxy method, sorted in order), and in terms of the above code, predicate returns true after the iteration returns the instance S.

Ok this is the way it is today, and this expression is often used in LINQ, allowing him to go deep into him in our application and practice.

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.