IEnumerable接口的Aggregate方法
以前小猪为了累加一个集合中的类容通常会写出类似这样的C#代码:
string result ="":
foreach (var item in items)
{
result+=item.centent;
}
大概意思就是遍历集合中的每一项来累加其中的一个值。今天小猪才发现其实.NET的集合已经提供了该功能:那就是小猪现在讲的IEnumerable<T>接口的Aggregate方法:
该方法提供了两个重载版本
版本1:Aggregate<TSource>(Func<TSource, TSource, TSource>):已重载。 对序列应用累加器函数。 (由 Enumerable 定义。)
版本2:Aggregate<TSource, TAccumulate>(TAccumulate, Func<TAccumulate, TSource, TAccumulate>)已重载。 对序列应用累加器函数。 将指定的种子值用作累加器初始值。 (由Enumerable 定义。)
public static TAccumulate Aggregate<TSource, TAccumulate>(
this IEnumerable<TSource> source,
TAccumulate seed,
Func<TAccumulate, TSource, TAccumulate> func
)
由定义可以看出该方法是个拓展方法,所以在使用时不需要传递第一个参数。
此方法的工作原理是对 source 中的每个元素调用一次 func。 每次调用 func 时,Aggregate<TSource, TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate, TSource, TAccumulate>) 都将传递序列中的元素和聚合值(作为 func 的第一个参数)。 将 seed 参数的值用作聚合的初始值。 用 func 的结果替换以前的聚合值。Aggregate<TSource, TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate, TSource, TAccumulate>) 返回 func 的最终结果。
若要简化一般的聚合运算,标准查询运算符还可以包含一个通用的计数方法(即 Count)和四个数值聚合方法(即 Min、Max、Sum 和 Average)。
下面的代码示例演示如何使用 Aggregate 应用累加器函数和使用种子值。
int[] ints = { 4, 8, 8, 3, 9, 0, 7, 8, 2 };
// Count the even numbers in the array, using a seed value of 0.
int numEven = ints.Aggregate(0, (total, next) =>next % 2 == 0 ? total + 1 : total);
Console.WriteLine("The number of even integers is: {0}", numEven);
// This code produces the following output:
//
// The number of even integers is: 6
IEnumerable接口的Aggregate方法的更多相关文章
- C# 索引器,实现IEnumerable接口的GetEnumerator()方法
当自定义类需要实现索引时,可以在类中实现索引器. 用Table作为例子,Table由多个Row组成,Row由多个Cell组成, 我们需要实现自定义的table[0],row[0] 索引器定义格式为 [ ...
- IEnumerable接口的扩展方法
/// <summary>/// IEnumerable接口的扩展方法,支持它的实现类是List的情况/// </summary>using System.Collection ...
- EF架构~为IEnumerable接口添加增删查等操作,原因是IEnumerable导航属性更放心
回到目录 对EF开发来说,导航属性肯定都用过,事实上,它是由VS IDE工具根据你的数据库关系结构自动生成的外键属性,在类视图中可以看到相关属性,它是以外键表名来标识的,如果是一对多的关系,那么,它会 ...
- 你可能不知道的陷阱, IEnumerable接口
1. IEnumerable 与 IEnumerator IEnumerable枚举器接口的重要性,说一万句话都不过分.几乎所有集合都实现了这个接口,Linq的核心也依赖于这个万能的接口.C语言的 ...
- foreach为什么要实现IEnumerable接口而不是直接用IEnumerator接口
在.Net中,要想被foreach遍历,那么目标对象要实现IEnumerable或IEnumerable<T>接口,这个接口有一个方法,GetEnumerator(),返回一个IEnume ...
- IEnumerable接口
IEnumerable接口顾名思义就是 可枚举的,可列举的. 接口也很简单,返回一个 枚举器对象 IEnumerator . [ComVisible(true), Guid("496B0AB ...
- IEnumerable接口的实现
对象要实现可以迭代需IEnumerable接口并实现GetEnumerator方法.一下简单例子 public class SPEnumerable<T> : IEnumerable { ...
- IEnumerable 接口 实现foreach 遍历 实例
额 为啥写着东西? 有次面试去,因为用到的时候特别少 所以没记住, 这个单词 怎么写! 经典的面试题: 能用foreach遍历访问的对象的要求? 答: 该类实现IEnumetable 接口 声明 ...
- IEnumerator/IEnumerable接口
IEnumberator函数成员 Current返回序列中当前位置项的 属性 只读属性 返回object类型 MoveNext把枚举器位置前进到集合中下一项的方法 新位置有效返回true,否则fals ...
随机推荐
- zjuoj 3602 Count the Trees
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3602 Count the Trees Time Limit: 2 Seco ...
- 提高网站性能的 5 个 Grunt任务//////////////////z
提高网站性能的 5 个 Grunt任务 时间 2015-03-06 09:13:02 极客头条 原文 http://hugnew.com/wordpress/提高网站性能的-5-个-grunt任务 ...
- 谨慎使用多线程中的fork
// Upon successful completion, pthread_atfork() shall return a value of zero; otherwise, an error nu ...
- 0511 backlog
SCRUM 这次的作业就是确定SCRUM的计划,确定sprint backlog的一个冲刺周期,而这个周期是两个星期.争取在两周内发布1.0版本. 本次作业以网站构建为主: ID NAME ...
- 提高Oracle的WHERE语句性能一些原则
索引是表的一个概念部分 , 用来提高检索数据的效率, ORACLE 使用了一个复杂的自平衡 B-tree 结构 . 通常 , 通过索引查询数据比全表扫描要快 . 当 ORACLE 找出执行查询和 Up ...
- Android PullToRefresh (ListView GridView 下拉刷新) 使用详解
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38238749,本文出自:[张鸿洋的博客] 群里一哥们今天聊天偶然提到这个git ...
- 如何在Macbook Pro搭建PHP开发环境
[Apache] sudo apachectl start // 启动Apache服务 sudo apachectl restart // 重启Apache服务 sudo apachectl s ...
- HDU 5688:2016"百度之星" - 资格赛 Problem D
原文链接:https://www.dreamwings.cn/hdu5688/2650.html Problem D Time Limit: 2000/1000 MS (Java/Others) ...
- javaScript实现修改输入框之后标红
<html> <title>实现标红</title> <script type="text/javascript"> functi ...
- Android SQLite数据库使用
在Android开发中SQLite起着很重要的作用,网上SQLite的教程有很多很多,不过那些教程大多数都讲得不是很全面.本人总结了一些SQLite的常用的方法,借着论坛的大赛,跟大家分享分享的.一. ...