product of values via Aggregate - CSharp LINQ

CSharp examples for LINQ:IEnumerable

Description

product of values via Aggregate

Demo Code

using System;//from w w  w .  j a  v  a 2  s  .  c o m
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
      // product of values via Aggregate
      Console.WriteLine("Product via Aggregate method: " + values.Aggregate(1, (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