C# 关于 IEnumerable<T>.Aggregate

标签 c# linq aggregate

我做了一些关于 IList<T>.Aggregate() 的测试,但答案对我来说没有意义。

List<int> Data1 = new List<int> { 1,0,0,0,0};

var result = Data1.Aggregate<int>((total, next) => total + total);

结果是16 .

我预计它会是 32 .

谁能解释一下?

最佳答案

Aggregate 不会对列表中的第一个元素运行回调。 相反,第一个元素用作累加器的初始值 (total)。
因此,您的回调仅运行四次,而不是五次,并且 24 = 16。

关于C# 关于 IEnumerable<T>.Aggregate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6064354/

相关文章:

C# EF6 LINQ 从 include() 中的子级中选择多个第二个子级

c# - 使用 LINQ 创建增量值的 IEnumerable<>

r - 如何对动物园对象中的多个列求和

python - 如何在Python中按总和和平均列进行分组?

python - MongoDB 条件(如果存在则求和,否则为零)

c# - 安装自签名证书无法解决 Windows 7 中的安全错误

c# - 如何在 Azure 移动应用服务中获取 Cosmos Db 数据

c# - 创建一个没有无限实例化的实例

c# - 从 ASP Web Api IHttpActionResult 反序列化 byte[]

c# - 在 linq foreach 中调用方法 - 有多少开销?