Skip to main content
.NET Framework Class Library
EnumerableAverageTSource Method (IEnumerableTSource, FuncTSource, NullableInt64)

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

Namespace:   System.Linq
Assembly:  System.Core (in System.Core.dll)
<[%$TOPIC/bb549137_en-us_VS_110_2_0_0_0_0%]> _
Public Shared Function Average(Of TSource) ( _
	source As [%$TOPIC/bb549137_en-us_VS_110_2_0_0_0_1%](Of TSource), _
	selector As [%$TOPIC/bb549137_en-us_VS_110_2_0_0_0_2%](Of TSource, [%$TOPIC/bb549137_en-us_VS_110_2_0_0_0_3%](Of [%$TOPIC/bb549137_en-us_VS_110_2_0_0_0_4%])) _
) As [%$TOPIC/bb549137_en-us_VS_110_2_0_0_0_5%](Of [%$TOPIC/bb549137_en-us_VS_110_2_0_0_0_6%])
public static [%$TOPIC/bb549137_en-us_VS_110_2_0_1_0_0%]<[%$TOPIC/bb549137_en-us_VS_110_2_0_1_0_1%]> Average<TSource>(
	this [%$TOPIC/bb549137_en-us_VS_110_2_0_1_0_2%]<TSource> source,
	[%$TOPIC/bb549137_en-us_VS_110_2_0_1_0_3%]<TSource, [%$TOPIC/bb549137_en-us_VS_110_2_0_1_0_4%]<[%$TOPIC/bb549137_en-us_VS_110_2_0_1_0_5%]>> selector
generic<typename TSource>
static [%$TOPIC/bb549137_en-us_VS_110_2_0_2_0_1%]<[%$TOPIC/bb549137_en-us_VS_110_2_0_2_0_2%]> Average(
	[%$TOPIC/bb549137_en-us_VS_110_2_0_2_0_3%]<TSource>^ source, 
	[%$TOPIC/bb549137_en-us_VS_110_2_0_2_0_4%]<TSource, [%$TOPIC/bb549137_en-us_VS_110_2_0_2_0_5%]<[%$TOPIC/bb549137_en-us_VS_110_2_0_2_0_6%]>>^ selector
static member Average : 
        source:[%$TOPIC/bb549137_en-us_VS_110_2_0_3_0_0%]<'TSource> * 
        selector:[%$TOPIC/bb549137_en-us_VS_110_2_0_3_0_1%]<'TSource, [%$TOPIC/bb549137_en-us_VS_110_2_0_3_0_2%]<[%$TOPIC/bb549137_en-us_VS_110_2_0_3_0_3%]>> -> [%$TOPIC/bb549137_en-us_VS_110_2_0_3_0_4%]<[%$TOPIC/bb549137_en-us_VS_110_2_0_3_0_5%]> 
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 to calculate the average of.

Type: SystemFuncTSource, Nullable Int64

A transform function to apply to each element.

Return Value

Type: SystemNullable Double
The average of the sequence of values, or if the source sequence is empty or contains only values that are .

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

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