gpt4 book ai didi

c# - 如何在 C# 中使用 Linq 将函数应用于列表中的每个元素,就像 python 中的方法 reduce() 一样?

转载 作者:太空宇宙 更新时间:2023-11-03 12:25:36 26 4
gpt4 key购买 nike

如何在 C# 中使用 Linq 将函数应用于列表中的每个元素,就像 python 中的方法 reduce() 一样?

最佳答案

假设您正在谈论 this reduce function ,C# 和 LINQ 中的等价物是 Enumerable.Aggregate .

简单示例:

var list = Enumerable.Range(5, 3); // [5, 6, 7]
Console.WriteLine("Aggregation: {0}", list.Aggregate((a, b) => (a + b)));
// Result is "Aggregation: 18"

关于c# - 如何在 C# 中使用 Linq 将函数应用于列表中的每个元素,就像 python 中的方法 reduce() 一样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3825200/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com