Wednesday, December 7, 2011

Linq calculate average of a list C#

Only one line of code will do it:

List<float> f = new List<float>();
for (int i = 0; i < 10; i++)
{
    f.Add((float)i);
}
float ave = f.Aggregate((acc, cur) => acc + cur) / f.Count;
Console.WriteLine(ave);

No comments:

Post a Comment