Summation using Lambda C# 3.0

Posted by Newbie on Stack Overflow See other posts from Stack Overflow or by Newbie
Published on 2010-04-23T02:26:14Z Indexed on 2010/04/23 2:33 UTC
Read the original article Hit count: 231

Filed under:

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.

© Stack Overflow or respective owner

Related posts about c#3.0