Computes the average of a sequence of nullable Decimal values that are obtained by invoking a transform function on each element of the input sequence.
Assembly: System.Core (in System.Core.dll)
<[%$TOPIC/bb534737_en-us_VS_110_2_0_0_0_0%]> _
Public Shared Function Average(Of TSource) ( _
source As [%$TOPIC/bb534737_en-us_VS_110_2_0_0_0_1%](Of TSource), _
selector As [%$TOPIC/bb534737_en-us_VS_110_2_0_0_0_2%](Of TSource, [%$TOPIC/bb534737_en-us_VS_110_2_0_0_0_3%](Of [%$TOPIC/bb534737_en-us_VS_110_2_0_0_0_4%])) _
) As [%$TOPIC/bb534737_en-us_VS_110_2_0_0_0_5%](Of [%$TOPIC/bb534737_en-us_VS_110_2_0_0_0_6%])
public static [%$TOPIC/bb534737_en-us_VS_110_2_0_1_0_0%]<[%$TOPIC/bb534737_en-us_VS_110_2_0_1_0_1%]> Average<TSource>(
this [%$TOPIC/bb534737_en-us_VS_110_2_0_1_0_2%]<TSource> source,
[%$TOPIC/bb534737_en-us_VS_110_2_0_1_0_3%]<TSource, [%$TOPIC/bb534737_en-us_VS_110_2_0_1_0_4%]<[%$TOPIC/bb534737_en-us_VS_110_2_0_1_0_5%]>> selector
)
[[%$TOPIC/bb534737_en-us_VS_110_2_0_2_0_0%]]
public:
generic<typename TSource>
static [%$TOPIC/bb534737_en-us_VS_110_2_0_2_0_1%]<[%$TOPIC/bb534737_en-us_VS_110_2_0_2_0_2%]> Average(
[%$TOPIC/bb534737_en-us_VS_110_2_0_2_0_3%]<TSource>^ source,
[%$TOPIC/bb534737_en-us_VS_110_2_0_2_0_4%]<TSource, [%$TOPIC/bb534737_en-us_VS_110_2_0_2_0_5%]<[%$TOPIC/bb534737_en-us_VS_110_2_0_2_0_6%]>>^ selector
)
static member Average :
source:[%$TOPIC/bb534737_en-us_VS_110_2_0_3_0_0%]<'TSource> *
selector:[%$TOPIC/bb534737_en-us_VS_110_2_0_3_0_1%]<'TSource, [%$TOPIC/bb534737_en-us_VS_110_2_0_3_0_2%]<[%$TOPIC/bb534737_en-us_VS_110_2_0_3_0_3%]>> -> [%$TOPIC/bb534737_en-us_VS_110_2_0_3_0_4%]<[%$TOPIC/bb534737_en-us_VS_110_2_0_3_0_5%]>
Type Parameters
- TSource
The type of the elements of source.
Parameters
- source
- Type:
System.Collections.GenericIEnumerableTSource
A sequence of values to calculate the average of.
- selector
- Type:
SystemFuncTSource,
Nullable
Decimal
A transform function to apply to each element.
Return Value
Type: SystemNullable DecimalThe 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).| Exception | Condition |
|---|---|
| ArgumentNullException | source or selector is . |
| OverflowException | 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.
Note |
|---|
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.
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Note