对序列应用累加器函数。
static TSource Aggregate<TSource>(
this IEnumerable<TSource> source,
Func<TSource, TSource, TSource> func
)
类型参数
- TSource
-
source 中的元素的类型。
返回值
类型:TSource累加器的最终值。
使用说明
在 Visual Basic 和 C# 中,可以在 扩展方法(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 的最终结果。
若要简化一般的聚合运算,标准查询运算符还可以包含一个通用的计数方法(即 Average)。
}
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);