2010-05-19 Jb Evain <jbevain@novell.com>
[mcs.git] / tests / gtest-lambda-15.cs
blobf4177514107618a8398763e69376e2b0df129007
1 using System;
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)
14 if (source == null)
15 throw new ArgumentNullException ("source");
16 if (selector == null)
17 throw new ArgumentNullException ("selector");
19 TR total = default (TR);
20 int counter = 0;
21 foreach (var element in source) {
22 total = selector (total, element);
23 ++counter;
26 if (counter == 0)
27 throw new InvalidOperationException ();
29 return total;
34 class Repro
37 static int Main ()
39 var sum = new [] { "1", "2", "3", "4", "5", "6", "7" }.Sum ((s) => int.Parse (s));
40 if (sum != 28)
41 return 4;
43 Console.WriteLine (sum);
44 return 0;