C# Average Extension Method

LINQ (language integrated query)

You have a collection of numbers and want to compute the average value using very short code. The Average extension method in the System.Linq extensions provides a declarative way to compute the average value of a sequence in the C# language.

Example

Note

First, programs that use the Average method require the "using System.Linq" directive to include the LINQ namespace. The Average method implementation is actually stored in the System.Core.dll file. Although the Average method looks like an instance method when called, it is not. It is an extension method.

Extension Method

This C# example program uses the Average extension method. It requires System.Linq.

Program that computes averages [C#]

using System;
using System.Linq;

class Program
{
    static void Main()
    {
	//
	// Use Average to compute average value.
	//
	double[] array1 = { 1, 2, 3, 5, 0 };
	double average1 = array1.Average();
	Console.WriteLine(average1);
	//
	// Use Average to compute average string length.
	//
	string[] array2 = { "dog", "cat", "perls" };
	double average2 = array2.Average(x => x.Length);
	Console.WriteLine(average2);
    }
}

Output

2.2
3.66666666666667
Double keyword

Overloads. There is one Average overload that accepts no parameters, and it requires a double IEnumerable implementation. There are more overloads that allow you to "select" the values you want to average from another collection with a selector delegate, which you can provide as a lambda expression.

Lambda Expression

Averaging numbers in double[] array. The first part of the program shows how you can directly average the values in a double[] collection, using the Average extension method. The Average method is equivalent to adding up the total of all of the numbers, and then dividing that total by the number of elements.

Selecting string length and averaging. The second part of the program shows how you use the Average method parameter to specify a function that will select a value from an existing sequence. The lambda expression "x => x.Length" can be read as "x goes to the length of x" which means you are extracting the length of the strings and then averaging those values.

Implementation

.NET Framework information

Here we explore the implementation of the Average extension method in the .NET Framework. If you provide a selector parameter to the method, it internally calls the Select extension method first. On that result, the implementation then checks the parameter for null, uses a foreach loop over the elements, and then divides the total value by the count.

It will throw an exception if it divides by zero. Because of all the overhead in this method, this implementation will be slower than directly accessing array elements and computing the average in a for-loop.

For Loops

Summary

The C# programming language

You can use the Average extension method in the C# programming language and System.Linq namespace to compute the average value of a collection of numbers. You can combine the Average extension with a Func selector parameter that will internally Select the elements you specify in the lambda expression. We saw an example of the Average method and then looked into the assembly where it is defined.

LINQ Examples
.NET