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);

1 comment:

  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Online Training from India. or learn thru ASP.NET Essential Training Online . Nowadays Dot Net has tons of job opportunities on various vertical industry.
    JavaScript Online Training from India

    ReplyDelete