.NET Framework Class Library
EnumerableAverageTSource Method (IEnumerableTSource, FuncTSource, Decimal)

Computes the average of a sequence of Decimal values that are obtained by invoking a transform function on each element of the input sequence.

Namespace:   System.Linq
Assemblies:   System.Linq (in System.Linq.dll)
  System.Core (in System.Core.dll)
<[%$TOPIC/bb549067_en-us_VS_110_2_0_0_0_0%]> _
Public Shared Function Average(Of TSource) ( _
	source As [%$TOPIC/bb549067_en-us_VS_110_2_0_0_0_1%](Of TSource), _
	selector As [%$TOPIC/bb549067_en-us_VS_110_2_0_0_0_2%](Of TSource, [%$TOPIC/bb549067_en-us_VS_110_2_0_0_0_3%]) _
) As [%$TOPIC/bb549067_en-us_VS_110_2_0_0_0_4%]
public static [%$TOPIC/bb549067_en-us_VS_110_2_0_1_0_0%] Average<TSource>(
	this [%$TOPIC/bb549067_en-us_VS_110_2_0_1_0_1%]<TSource> source,
	[%$TOPIC/bb549067_en-us_VS_110_2_0_1_0_2%]<TSource, [%$TOPIC/bb549067_en-us_VS_110_2_0_1_0_3%]> selector
generic<typename TSource>
static [%$TOPIC/bb549067_en-us_VS_110_2_0_2_0_1%] Average(
	[%$TOPIC/bb549067_en-us_VS_110_2_0_2_0_2%]<TSource>^ source, 
	[%$TOPIC/bb549067_en-us_VS_110_2_0_2_0_3%]<TSource, [%$TOPIC/bb549067_en-us_VS_110_2_0_2_0_4%]>^ selector
static member Average : 
        source:[%$TOPIC/bb549067_en-us_VS_110_2_0_3_0_0%]<'TSource> * 
        selector:[%$TOPIC/bb549067_en-us_VS_110_2_0_3_0_1%]<'TSource, [%$TOPIC/bb549067_en-us_VS_110_2_0_3_0_2%]> -> [%$TOPIC/bb549067_en-us_VS_110_2_0_3_0_3%]
JScript does not support generic types or methods.

Type Parameters


The type of the elements of source.


Type: System.Collections.GenericIEnumerableTSource

A sequence of values that are used to calculate an average.

Type: SystemFuncTSource, Decimal

A transform function to apply to each element.

Return Value

Type: SystemDecimal
The average of the sequence of values.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IEnumerableTSource. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).

source or selector is .


source contains no elements.


The sum of the elements in the sequence is larger than MaxValue.


In Visual Basic query expression syntax, an Aggregate Into Average() clause translates to an invocation of Average.


The following code example demonstrates how to use AverageTSource(IEnumerableTSource, FuncTSource, Int64) to calculate an average.


This code example uses an overload of this overloaded method that is different from the specific overload that this topic describes. To extend the example to this topic, change the body of the selector function.

        ' Create an array of strings. 
        Dim numbers() As String = {"10007", "37", "299846234235"}

        ' Determine the average number after converting each 
        ' string to an Int64 value. 
        Dim avg As Double = _
            numbers.Average(Function(number) Convert.ToInt64(number))

        ' Display the output.
        MsgBox("The average is " & avg)

        ' This code produces the following output: 
        ' The average is 99948748093
string[] numbers = { "10007", "37", "299846234235" };

            double average = numbers.Average(num => Convert.ToInt64(num));

            Console.WriteLine("The average is {0}.", average);

            // This code produces the following output: 
            // The average is 99948748093. 
Version Information

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8