2 using System.Collections.Generic;
4 static class Enumerable
7 public static int Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, int> selector)
9 return Sum<TSource, int> (source, (a, b) => a + selector (b));
12 static TR Sum<TA, TR> (this IEnumerable<TA> source, Func<TR, TA, TR> selector)
15 throw new ArgumentNullException ("source");
17 throw new ArgumentNullException ("selector");
19 TR total = default (TR);
21 foreach (var element in source) {
22 total = selector (total, element);
27 throw new InvalidOperationException ();
37 public static int Main ()
39 var sum = new [] { "1", "2", "3", "4", "5", "6", "7" }.Sum ((s) => int.Parse (s));
43 Console.WriteLine (sum);