C# 中的 Aggregate 方法
在 C# 中使用 Aggregate 方法进行数学运算,例如求和、求最小值、求最大值、求平均值等。
我们来看一个使用 Aggregate 方法计算数组元素乘积的示例。
这是我们的数组 -
int[] arr = { 10, 15, 20 };现在,使用 Aggregate() 方法 -
arr.Aggregate((x, y) => x * y);
以下是完整代码 -
範例
using System;
using System.Linq;
using System.IO;
public class Demo {
public static void Main() {
int[] arr = { 10, 15, 20 };
// Multiplication
int res = arr.Aggregate((x, y) => x * y);
Console.WriteLine(res);
}
}产出
3000
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP