C# Enumerable Average(IEnumerable, Func)
Description
Computes the average of a sequence of Decimal values that are obtained by invoking a transform function on each element of the input sequence.
Syntax
public static decimal Average<TSource>(
this IEnumerable<TSource> source,
Func<TSource, decimal> selector
)
Parameters
TSource
- The type of the elements of source.source
- A sequence of values that are used to calculate an average.selector
- A transform function to apply to each element.
Returns
returns The average of the sequence of values.
Example
The following code example demonstrates how to use Average(IEnumerable) to calculate an average.
/* w w w .j a v a 2 s .c o m*/
using System;
using System.Linq;
using System.Collections.Generic;
public class MainClass{
public static void Main(String[] argv){
string[] numbers = { "10007", "37", "123123123123" };
double average = numbers.Average(num => Convert.ToInt64(num));
Console.WriteLine("The average is {0}.", average);
}
}
The code above generates the following result.