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

对序列应用累加器函数。

 

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)。

 

 

ContractedBlock.gif ExpandedBlockStart.gif Code
static void Main(string[] args)
        {
            
string sentence = "the quick brown fox jumps over the lazy dog";

            
// Split the string into individual words.
            string[] words = sentence.Split(' ');

            
// Prepend each word to the beginning of the 
            
// new sentence to reverse the word order.
            string reversed = words.Aggregate(Converters);

            Console.WriteLine(reversed);



        }

        
public static string Converters(string input1,string input2)
        {
            
string a = input1;
            
string b = input2;
            Console.WriteLine(input2 
+ " " + input1);
            
return input2 + " " + input1;
        }

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);

 

转载于:https://www.cnblogs.com/joe-yang/archive/2009/06/05/1497241.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值