Mega Code Archive

 
Categories / C# Book / 04 LINQ
 

0388 Aggregate

Aggregate allows you to specify a custom accumulation algorithm for implementing aggregations. The following demonstrates how Aggregate can do the work of Sum: using System; using System.Collections; using System.Collections.Generic; using System.Linq; class Program { static void Main() { int[] numbers = { 2, 3, 4 }; int sum = numbers.Aggregate(0, (total, n) => total + n); // 9 Console.WriteLine(sum); } } The output: 9