I've looked all over but can't figure this out. How do you sum a list of BigIntegers?
Using System.Numerics;
Using System.Linq;
List<BigInteger> bigInts = new List<BigInteger>();
BigInteger sum = bigInts.Sum(); // doesn't work
BigInteger sum = bigInts.Sum<BigInteger>(); // doesn't work
BigInteger sum = bigInts.Sum(x => x); // doesn't work
Do you have to do this?
BigInteger sum = new BigInteger(0);
foreach(BigInteger bigint in bigInts)
sum += bigint;
Aggregate gets a delegate to a method which gets two BigIntegers and return a BigInteger. It uses a default BigInteger as initial value (0), and goes over each BigInteger, invoking BigInteger.Add with the previous result (0 would be previous result in the first time - also called 'seed') and the current element.
As Alexei said Aggregate is the more general from of sum. Presented below is an extension method.
I haven't tested this, and my C# might be getting a little rusty. but the idea should be sound: see http://msdn.microsoft.com/en-us/library/bb549218.aspx#Y0
You can also use the ForEach() method on generic lists to do the addition:
Aggregate function is more general version of Sum: