C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
However: The second call is more interesting. It multiplies the accumulation with the next element.
Accumulate: With Aggregate we "accumulate" values, meaning we build up values from each previous function call.
C# program that calls Aggregate method
using System;
using System.Linq;
class Program
{
static void Main()
{
int[] array = { 1, 2, 3, 4, 5 };
int result = array.Aggregate((a, b) => b + a);
// 1 + 2 = 3
// 3 + 3 = 6
// 6 + 4 = 10
// 10 + 5 = 15
Console.WriteLine(result);
result = array.Aggregate((a, b) => b * a);
// 1 * 2 = 2
// 2 * 3 = 6
// 6 * 4 = 24
// 24 * 5 = 120
Console.WriteLine(result);
}
}
Output
15
120
So: The list of methods, starting with Aggregate, are methods from a namespace called System.Linq.
ExtensionHowever: In my experience, Aggregate is one of the least useful extensions. Try using ToArray or ToList to gain familiarity.
ToArrayToList