Loading

Enumerable..::.Aggregate<(Of <(TSource>)>) 方法 (IEnumerable<(Of <(TSource>)>), Func<(Of <(TSource, TSource, TSource>)>))

对序列应用累加器函数。

 

C#
public static TSource Aggregate<TSource>(
    this IEnumerable<TSource> source,
    Func<TSource, TSource, TSource> func
)

 

类型参数

TSource

source 中的元素的类型。

参数

source
类型:System.Collections.Generic..::.IEnumerable<(Of <(TSource>)>)

要聚合的 IEnumerable<(Of <(T>)>)

func
类型:System..::.Func<(Of <(TSource, TSource, TSource>)>)

要对每个元素调用的累加器函数。

返回值

类型:TSource

累加器的最终值。

使用说明

在 Visual Basic 和 C# 中,可以在 IEnumerable<(Of <(TSource>)>) 类型的任何对象上将此方法作为实例方法来调用。当使用实例方法语法调用此方法时,请省略第一个参数。有关更多信息,请参见扩展方法 (Visual Basic)扩展方法(C# 编程指南)

 

 

Aggregate<(Of <(TSource>)>)(IEnumerable<(Of <(TSource>)>), Func<(Of <(TSource, TSource, TSource>)>)) 方法可简化在值序列上执行的计算。此方法的工作原理是对 source 中的每个元素调用一次 func每次调用 func 时,Aggregate<(Of <(TSource>)>)(IEnumerable<(Of <(TSource>)>), Func<(Of <(TSource, TSource, TSource>)>)) 都将传递序列中的元素和聚合值(作为 func 的第一个参数)。将 source 的第一个元素用作聚合的初始值。func 的结果将替换以前的聚合值。Aggregate<(Of <(TSource>)>)(IEnumerable<(Of <(TSource>)>), Func<(Of <(TSource, TSource, TSource>)>)) 返回 func 的最终结果。

若要简化一般的聚合运算,标准查询运算符还可以包含一个通用的计数方法(即 Count)和四个数值聚合方法(即 MinMaxSumAverage)。

 

 

Code

output:

-------------

 quick the
brown quick the
fox brown quick the
jumps fox brown quick the
over jumps fox brown quick the
the over jumps fox brown quick the
lazy the over jumps fox brown quick the
dog lazy the over jumps fox brown quick the
dog lazy the over jumps fox brown quick the
请按任意键继续. . .

 

 

//简写
string reversed = words.Aggregate((workingSentence, next) =>
                                      next 
+ " " + workingSentence);

 

posted @ 2009-06-05 20:28  .net's  阅读(1749)  评论(0编辑  收藏  举报