Friday, June 17, 2011

Aggregate OPERATORS

================================================
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

int oddNumbers = numbers.Count(n => n % 2 == 1);

Console.WriteLine("There are {0} odd numbers in the list.", oddNumbers);
================================================
double startBalance = 100.0;

int[] attemptedWithdrawals = { 20, 10, 40, 50, 10, 70, 30 };

double endBalance =
attemptedWithdrawals.Aggregate(startBalance,
(balance, nextWithdrawal) =>
((nextWithdrawal <= balance) ? (balance - nextWithdrawal) : balance));

Console.WriteLine("Ending balance: {0}", endBalance);
=================================================

No comments:

Post a Comment