#150 – Other Aggregate Functions You Can Apply to Numeric Arrays

Post #147 mentioned using IEnumerable.Average to get the average of an array of numeric values.  The System.Linq namespace includes several other extension methods for IEnumerable that can be used to aggregate values:

  • Max – Get the maximum value
  • Min – Get the minimum value
  • Sum – Get the sum of all values
            int[] scores = { 89, 98, 72, 100, 83 };
            int sum = scores.Sum();        // 442
            int min = scores.Min();        // 72
            int max = scores.Max();        // 100
            double avg = scores.Average();       // 88.4
Advertisements