.NET Framework Average

Download .NET Framework for free

Example

var numbers = new[] {1,2,3,4};

var averageNumber = numbers.Average();
Console.WriteLine(averageNumber); 
// 2,5

This method calculates the average of enumerable of numbers.

var cities = new[] {
    new {Population = 1000},
    new {Population = 2000},
    new {Population = 4000}
};

var averagePopulation = cities.Average(c => c.Population);
Console.WriteLine(averagePopulation);
// 2333,33

This method calculates the average of enumerable using delegated function.

Cast