Aggregate method in C#


Use the Aggregate method in C# to perform mathematical operations such as Sum, Min, Max, Average, etc.

Let us see an example to multiply array elements using Aggregate method.

Here is our array −

int[] arr = { 10, 15, 20 };

Now, use Aggregate() method −

arr.Aggregate((x, y) => x * y);

Here is the complete code −

Example

 Live Demo

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

Output

3000

Updated on: 22-Jun-2020

821 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements