Aggregate

Aggregate functions are useful when you need to perform a specified calculation on all elements in a list. A common type of aggregation is a sum of all numbers in a list.

Basic example

The example below uses the Aggregate method to achieve this. Just so you know, using the Sum method produces the same result.

Live example: https://dotnetfiddle.net/gGM2wz

var numbers = new[] { 2, 4, 6, 8, 10};

var result = numbers.Aggregate((x, y) => x + y);

// (2 + 4) = 6, (6 + 6) = 12, (12 + 8) = 20, (20 + 10) = 30

var sum = numbers.Sum();

//output: 30