LINQ series: Fundamentals and Nature (part II)

Source: Internet
Author: User

And then, we often mention two things in the last article (LINQ Series: Basics and Nature (part I)), System.Linq.Enumerable and System.Func. So what exactly are two things? Let's look at the following code:

When a Linq expression is compiled, the compiler converts the query operator to a call (or other type) for several functions in the System.Linq.Enumerable type. Here are a few enumerable member functions:

Code

public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source,
  System.Func<TSource,int,bool> predicate)
public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source,
  System.Func<TSource,bool> predicate)
public static int? Sum( this IEnumerable<int?> source );

We can see that most of these functions require proxies as arguments, and in particular, generic proxy func<> defined in System.Core.dll, a large part of which is a new feature of c#3.x: Extension method. Interested friends can go and have a look.

Let's look at the definition of func<>: The last type parameter TResult represents the return type:

Code

1 public delegate TResult Func<T0,T1,T2,T3,TResult>(
2 T0 arg0, T1 arg1, T2 arg2, T3 arg3)
3 public delegate TResult Func<T0,T1,T2,TResult>(T0 arg0, T1 arg1, T2 arg2)
4 public delegate TResult Func<T0,T1,TResult>(T0 arg0, T1 arg1)
5 public delegate TResult Func<T0,TResult>(T0 arg0)
6 public delegate TResult Func<TResult>()

Because many of the member functions of System.Linq.Enumerable require proxies as arguments, we can implement them in the following ways when invoked:

Define a query expression using the query operator

This is the most straightforward and concise way to refer to the example code for the previous article (LINQ Series: Basics and Nature (part I)).

Use enumerable type functions and lambda expressions to define query expressions

Refer to the example code for the previous article (LINQ Series: Fundamentals and Nature (part I)).

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.