I have a small thing.
public int GetSum(List<int> x)
{
foreach (int i in x) sum += i;
return sum;
}
where x is a List of integers defined as
List<int> l = new List<int>();
l.Add(4); l.Add(2); l.Add(5); l.Add(8); l.Add(6);
and has been passed as GetSum(l)
Is it possible to rewrite the above foreach loop using a lambda?
Reason: I started looking into the lambda stuffs since yesterday and is interested to learn .. however simple it may be.
Thanks.
LINQ already has a
Sumextension method:You can rewrite your
GetSummethod using C# 2.0’sList<T>.ForEachmethod, like this: