sum of values via Aggregate - CSharp LINQ

CSharp examples for LINQ:IEnumerable

Description

sum of values via Aggregate

Demo Code

using System;//from  w  w  w  .j av  a2  s  .com
using System.Collections.Generic;
using System.Linq;
class FunctionalProgramming
{
   static void Main()
   {
      var values = new List<int> {3, 10, 6, 1, 4, 8, 2, 5, 9, 7};
      Console.Write("Original values: ");
      values.Display(); // call Display extension method
      // sum of values via Aggregate
      Console.WriteLine("\nSum via Aggregate method: " + values.Aggregate(0, (x, y) => x + y));
   }
}
// declares an extension method
static class Extensions
{
   // extension method that displays all elements separated by spaces
   public static void Display<T>(this IEnumerable<T> data)
   {
      Console.WriteLine(string.Join(" ", data));
   }
}

Result


Related Tutorials