Lambda延迟执行和立即执行 [英] Lambda- deferred execution and immediate execution

查看:107
本文介绍了Lambda延迟执行和立即执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

专家们,我正在研究lambda,我只是发现有延迟执行和立即执行?

experts, I am studying lambda, I just find that there is deferred execution and immediate execution ?

然后我检查并转到定义"并找到

then I check and ' go to definition' and find

我可以说,如果返回类型不是IEnumerable或IOrderedEnumerable等(例如,int,long,double,....,TSource),然后 方法是立即执行?
如果返回类型为IEnumerable或IOrderedEnumerable等等,那么该方法将推迟执行?

can I say that if the return type is NOT IEnumerable or IOrderedEnumerable , etc, (i.e. e.g, int, long, double, ...., TSource) then the method is immediate execution?
if the the return type is IEnumerable or IOrderedEnumerable , etc, then the method is deferred execution?

#region Assembly System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Core.dll
#endregion

using System.Collections;
using System.Collections.Generic;

namespace System.Linq
{
public static class Enumerable
{
public static TSource Aggregate(this IEnumerable source, Func func);
public static TAccumulate Aggregate(this IEnumerable source, TAccumulate seed, Func func);
public static TResult Aggregate(this IEnumerable source, TAccumulate seed, Func func, Func resultSelector);
public static bool All(this IEnumerable source, Func predicate);
public static bool Any(this IEnumerable source);
public static bool Any(this IEnumerable source, Func predicate);
public static IEnumerable AsEnumerable(this IEnumerable source);
public static decimal? Average(this IEnumerable source, Func selector);
public static decimal Average(this IEnumerable source, Func selector);
public static double Average(this IEnumerable source);
public static double? Average(this IEnumerable source);
public static double Average(this IEnumerable source);
public static double? Average(this IEnumerable source);
public static float Average(this IEnumerable source);
public static double Average(this IEnumerable source);
public static double? Average(this IEnumerable source);
public static decimal Average(this IEnumerable source);
public static decimal? Average(this IEnumerable source);
public static double Average(this IEnumerable source, Func selector);
public static double? Average(this IEnumerable source, Func selector);
public static double Average(this IEnumerable source, Func selector);
public static double? Average(this IEnumerable source, Func selector);
public static float Average(this IEnumerable source, Func selector);
public static float? Average(this IEnumerable source, Func selector);
public static double Average(this IEnumerable source, Func selector);
public static double? Average(this IEnumerable source, Func selector);
public static float? Average(this IEnumerable source);
public static IEnumerable Cast(this IEnumerable source);
public static IEnumerable Concat(this IEnumerable first, IEnumerable second);
public static bool Contains(this IEnumerable source, TSource value);
public static bool Contains(this IEnumerable source, TSource value, IEqualityComparer comparer);
public static int Count(this IEnumerable source, Func predicate);
public static int Count(this IEnumerable source);
public static IEnumerable DefaultIfEmpty(this IEnumerable source);
public static IEnumerable DefaultIfEmpty(this IEnumerable source, TSource defaultValue);
public static IEnumerable Distinct(this IEnumerable source, IEqualityComparer comparer);
public static IEnumerable Distinct(this IEnumerable source);
public static TSource ElementAt(this IEnumerable source, int index);
public static TSource ElementAtOrDefault(this IEnumerable source, int index);
public static IEnumerable Empty();
public static IEnumerable Except(this IEnumerable first, IEnumerable second);
public static IEnumerable Except(this IEnumerable first, IEnumerable second, IEqualityComparer comparer);
public static TSource First(this IEnumerable source, Func predicate);
public static TSource First(this IEnumerable source);
public static TSource FirstOrDefault(this IEnumerable source, Func predicate);
public static TSource FirstOrDefault(this IEnumerable source);
public static IEnumerable<IGrouping> GroupBy(this IEnumerable source, Func keySelector);
public static IEnumerable<IGrouping> GroupBy(this IEnumerable source, Func keySelector, IEqualityComparer comparer);
public static IEnumerable GroupBy(this IEnumerable source, Func keySelector, Func<TKey, IEnumerable, TResult> resultSelector);
public static IEnumerable GroupBy(this IEnumerable source, Func keySelector, Func elementSelector, Func<TKey, IEnumerable, TResult> resultSelector);
public static IEnumerable GroupBy(this IEnumerable source, Func keySelector, Func<TKey, IEnumerable, TResult> resultSelector, IEqualityComparer comparer);
public static IEnumerable GroupBy(this IEnumerable source, Func keySelector, Func elementSelector, Func<TKey, IEnumerable, TResult> resultSelector, IEqualityComparer comparer);
public static IEnumerable<IGrouping> GroupBy(this IEnumerable source, Func keySelector, Func elementSelector, IEqualityComparer comparer);
public static IEnumerable<IGrouping> GroupBy(this IEnumerable source, Func keySelector, Func elementSelector);
public static IEnumerable GroupJoin(this IEnumerable outer, IEnumerable inner, Func outerKeySelector, Func innerKeySelector, Func<TOuter, IEnumerable, TResult> resultSelector);
public static IEnumerable GroupJoin(this IEnumerable outer, IEnumerable inner, Func outerKeySelector, Func innerKeySelector, Func<TOuter, IEnumerable, TResult> resultSelector, IEqualityComparer comparer);
public static IEnumerable Intersect(this IEnumerable first, IEnumerable second);
public static IEnumerable Intersect(this IEnumerable first, IEnumerable second, IEqualityComparer comparer);
public static IEnumerable Join(this IEnumerable outer, IEnumerable inner, Func outerKeySelector, Func innerKeySelector, Func resultSelector);
public static IEnumerable Join(this IEnumerable outer, IEnumerable inner, Func outerKeySelector, Func innerKeySelector, Func resultSelector, IEqualityComparer comparer);
public static TSource Last(this IEnumerable source, Func predicate);
public static TSource Last(this IEnumerable source);
public static TSource LastOrDefault(this IEnumerable source, Func predicate);
public static TSource LastOrDefault(this IEnumerable source);
public static long LongCount(this IEnumerable source, Func predicate);
public static long LongCount(this IEnumerable source);
public static double Max(this IEnumerable source);
public static decimal Max(this IEnumerable source);
public static decimal? Max(this IEnumerable source);
public static decimal Max(this IEnumerable source, Func selector);
public static double? Max(this IEnumerable source, Func selector);
public static TSource Max(this IEnumerable source);
public static int Max(this IEnumerable source);
public static double Max(this IEnumerable source, Func selector);
public static float? Max(this IEnumerable source, Func selector);
public static int? Max(this IEnumerable source);
public static long Max(this IEnumerable source);
public static float Max(this IEnumerable source, Func selector);
public static long? Max(this IEnumerable source, Func selector);
public static long Max(this IEnumerable source, Func selector);
public static int? Max(this IEnumerable source, Func selector);
public static int Max(this IEnumerable source, Func selector);
public static float? Max(this IEnumerable source);
public static double? Max(this IEnumerable source);
public static long? Max(this IEnumerable source);
public static float Max(this IEnumerable source);
public static TResult Max(this IEnumerable source, Func selector);
public static decimal? Max(this IEnumerable source, Func selector);
public static int? Min(this IEnumerable source);
public static long Min(this IEnumerable source);
public static long? Min(this IEnumerable source);
public static float Min(this IEnumerable source);
public static float? Min(this IEnumerable source);
public static double Min(this IEnumerable source);
public static double? Min(this IEnumerable source);
public static decimal Min(this IEnumerable source);
public static decimal? Min(this IEnumerable source);
public static TSource Min(this IEnumerable source);
public static int Min(this IEnumerable source, Func selector);
public static int? Min(this IEnumerable source, Func selector);
public static long Min(this IEnumerable source, Func selector);
public static long? Min(this IEnumerable source, Func selector);
public static float Min(this IEnumerable source, Func selector);
public static float? Min(this IEnumerable source, Func selector);
public static double Min(this IEnumerable source, Func selector);
public static double? Min(this IEnumerable source, Func selector);
public static decimal Min(this IEnumerable source, Func selector);
public static int Min(this IEnumerable source);
public static decimal? Min(this IEnumerable source, Func selector);
public static TResult Min(this IEnumerable source, Func selector);
public static IEnumerable OfType(this IEnumerable source);
public static IOrderedEnumerable OrderBy(this IEnumerable source, Func keySelector, IComparer comparer);
public static IOrderedEnumerable OrderBy(this IEnumerable source, Func keySelector);
public static IOrderedEnumerable OrderByDescending(this IEnumerable source, Func keySelector, IComparer comparer);
public static IOrderedEnumerable OrderByDescending(this IEnumerable source, Func keySelector);
public static IEnumerable Range(int start, int count);
public static IEnumerable Repeat(TResult element, int count);
public static IEnumerable Reverse(this IEnumerable source);
public static IEnumerable Select(this IEnumerable source, Func selector);
public static IEnumerable Select(this IEnumerable source, Func selector);
public static IEnumerable SelectMany(this IEnumerable source, Func<TSource, IEnumerable> selector);
public static IEnumerable SelectMany(this IEnumerable source, Func<TSource, IEnumerable> collectionSelector, Func resultSelector);
public static IEnumerable SelectMany(this IEnumerable source, Func<TSource, int, IEnumerable> collectionSelector, Func resultSelector);
public static IEnumerable SelectMany(this IEnumerable source, Func<TSource, int, IEnumerable> selector);
public static bool SequenceEqual(this IEnumerable first, IEnumerable second, IEqualityComparer comparer);
public static bool SequenceEqual(this IEnumerable first, IEnumerable second);
public static TSource Single(this IEnumerable source, Func predicate);
public static TSource Single(this IEnumerable source);
public static TSource SingleOrDefault(this IEnumerable source);
public static TSource SingleOrDefault(this IEnumerable source, Func predicate);
public static IEnumerable Skip(this IEnumerable source, int count);
public static IEnumerable SkipWhile(this IEnumerable source, Func predicate);
public static IEnumerable SkipWhile(this IEnumerable source, Func predicate);
public static int? Sum(this IEnumerable source, Func selector);
public static decimal Sum(this IEnumerable source, Func selector);
public static decimal? Sum(this IEnumerable source, Func selector);
public static int Sum(this IEnumerable source);
public static int? Sum(this IEnumerable source);
public static long Sum(this IEnumerable source);
public static long? Sum(this IEnumerable source);
public static float Sum(this IEnumerable source);
public static float? Sum(this IEnumerable source);
public static double? Sum(this IEnumerable source);
public static double Sum(this IEnumerable source);
public static decimal? Sum(this IEnumerable source);
public static int Sum(this IEnumerable source, Func selector);
public static long Sum(this IEnumerable source, Func selector);
public static long? Sum(this IEnumerable source, Func selector);
public static float Sum(this IEnumerable source, Func selector);
public static float? Sum(this IEnumerable source, Func selector);
public static double Sum(this IEnumerable source, Func selector);
public static double? Sum(this IEnumerable source, Func selector);
public static decimal Sum(this IEnumerable source);
public static IEnumerable Take(this IEnumerable source, int count);
public static IEnumerable TakeWhile(this IEnumerable source, Func predicate);
public static IEnumerable TakeWhile(this IEnumerable source, Func predicate);
public static IOrderedEnumerable ThenBy(this IOrderedEnumerable source, Func keySelector);
public static IOrderedEnumerable ThenBy(this IOrderedEnumerable source, Func keySelector, IComparer comparer);
public static IOrderedEnumerable ThenByDescending(this IOrderedEnumerable source, Func keySelector);
public static IOrderedEnumerable ThenByDescending(this IOrderedEnumerable source, Func keySelector, IComparer comparer);
public static TSource[] ToArray(this IEnumerable source);
public static Dictionary ToDictionary(this IEnumerable source, Func keySelector, Func elementSelector, IEqualityComparer comparer);
public static Dictionary ToDictionary(this IEnumerable source, Func keySelector, Func elementSelector);
public static Dictionary ToDictionary(this IEnumerable source, Func keySelector, IEqualityComparer comparer);
public static Dictionary ToDictionary(this IEnumerable source, Func keySelector);
public static List ToList(this IEnumerable source);
public static ILookup ToLookup(this IEnumerable source, Func keySelector);
public static ILookup ToLookup(this IEnumerable source, Func keySelector, Func elementSelector);
public static ILookup ToLookup(this IEnumerable source, Func keySelector, Func elementSelector, IEqualityComparer comparer);
public static ILookup ToLookup(this IEnumerable source, Func keySelector, IEqualityComparer comparer);
public static IEnumerable Union(this IEnumerable first, IEnumerable second, IEqualityComparer comparer);
public static IEnumerable Union(this IEnumerable first, IEnumerable second);
public static IEnumerable Where(this IEnumerable source, Func predicate);
public static IEnumerable Where(this IEnumerable source, Func predicate);
public static IEnumerable Zip(this IEnumerable first, IEnumerable second, Func resultSelector);
}
}


delaynomore.

delaynomore.

推荐答案

我认为您混淆了Lambda表达式和LINQ,尽管它们是相关的(LINQ-语言集成查询-大量使用lambda表达式.

I think you are confusing Lambda expressions and LINQ, although they are related (LINQ - Language Intergrated Queries - make heavy use of lambda expressions).

lambda表达式只是一种编写小的内联函数的方法,您可以将其作为参数传递.例如,考虑:

A lambda expression is just a way of writing a small in-line function that you can pass on as an argument. For example, consider:

public void SomeMethod(Action<int> outputInt)
{
   for (int i=0; i<10; i++)
   {
       outputInt(i);
   }
}

此方法要求将另一个方法作为outputInt传入(Action< int>是定义为采用一个整数参数的方法的委托).

This method requires another method to be passed in as the outputInt (Action<int> is a delegate defined as a method that takes one integer parameter).

因此,当您调用SomeMethod时,您需要传入另一个采用int的方法.但是,您可以使用lambda来代替显式定义其他方法:

So when you call SomeMethod you need to pass in another method that takes an int. But rather than define that other methods explicitly, you can use a lambda:

SomeMethod( i => Console.WriteLine(i) );

或者...更简单

SomeMethod(Console.WriteLine);

请注意,当我传入Console.WriteLine时,它不会被调用.我只是将该方法(如函数指针")传入SomeMethod,该方法在循环中自行执行.

Note that Console.WriteLine is not called when I passed it in. I am simply passing in that method (like a 'function pointer') into SomeMethod, which executes it itself within the loop.

现在... LINQ.这做类似的事情.您设置了LINQ查询,通常会传递lamda表达式,但是直到必要时,这些表达式和LINQ查询本身才会执行.这是延迟执行,它比LINQ更重要的是LINQ的一个方面. lambda表达式本身.

Now...LINQ. This does a similar thing. You set up a LINQ query, quite often passing in lamda expressions, but those expressions and the LINQ query itself will not be executed until necessary. This is deferred execution and is more a aspect of LINQ than the lambda expressions themselves.

看看 这里

Have a look here

如果您点击该链接,则会看到一个示例LINQ表达式(以特殊语法编写,但实际上是使用lamda表达式构造的,例如,"i%2 == 0"位是传递给哪里"方法):

If you follow that link you'll see an example LINQ expression (written in a special syntax, but really constructed with lamda expressions, e.g. the "i%2 ==0" bit is a lambda expression passed to the 'Where' method):

var query = from i in ienum
            where i%2 == 0
            select i;

定义查询.但是,直到需要时才实际执行任何操作.也就是说,当您实际上开始循环查询以选择结果时.

This defines the query. But nothing is actually executed until required. That is, when you actually start looping around the query to select the results. 


这篇关于Lambda延迟执行和立即执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆