{TOC}
| Namespace: | System.Linq |
| Assembly: | System.Core.dll |
| Extends: | IEnumerable<T> |
Back to
Standard Query Operator IndexEditIntroduction
The Average operator computes the average of a sequence of numeric values.
EditMethod Signatures
// 1 - Average all elements
public static decimal Average(
this IEnumerable<decimal> source)
public static double Average(
this IEnumerable<int> source)
public static Nullable<decimal> Average(
this IEnumerable<Nullable<decimal>> source)
public static Nullable<double> Average(
this IEnumerable<Nullable<double>> source)
public static double Average(
this IEnumerable<long> source)
public static Nullable<double> Average(
this IEnumerable<Nullable<int>> source)
public static double Average(
this IEnumerable<double> source)
public static Nullable<double> Average(
this IEnumerable<Nullable<long>> source)
public static Nullable<float> Average(
this IEnumerable<Nullable<float>> source)
public static float Average(
this IEnumerable<float> source)
// 2 - Average all elements derived from a selector function.
public static decimal Average<TSource>(
this IEnumerable<TSource> source,
Func<TSource, decimal> selector)
public static double Average<TSource>(
this IEnumerable<TSource> source,
Func<TSource, double> selector)
public static double Average<TSource>(
this IEnumerable<TSource> source,
Func<TSource, int> selector)
public static double Average<TSource>(
this IEnumerable<TSource> source,
Func<TSource, long> selector)
public static Nullable<decimal> Average<TSource>(
this IEnumerable<TSource> source,
Func<TSource, Nullable<decimal>> selector)
public static Nullable<double> Average<TSource>(
this IEnumerable<TSource> source,
Func<TSource, Nullable<double>> selector)
public static Nullable<double> Average<TSource>(
this IEnumerable<TSource> source,
Func<TSource, Nullable<int>> selector)
public static Nullable<double> Average<TSource>(
this IEnumerable<TSource> source,
Func<TSource, Nullable<long>> selector)
public static Nullable<float> Average<TSource>(
this IEnumerable<TSource> source,
Func<TSource, Nullable<float>> selector)
public static float Average<TSource>(
this IEnumerable<TSource> source,
Func<TSource, float> selector)EditExceptions
Throws an ArgumentNullException if
source or
selector is null.
Throws an InvalidOperationException if there are no elements in
source.
EditPseudo-code
Overload 1
If
source is null throw ArgumentNullException.
If
source has no elements.
If the TSource is a nullable type.
Make the highest value a nullable TSource instance.
Else.
Throw an InvalidOperationException.
Initialize running total: sum as 0, and element counter: count as 0.
Iterate over the
source elements
If the current element has a value (i.e. Is not null)
sum = previous sum value + this elements value
count = previous count + 1
Return sum / count.
Overload 2 - With selector.
If
source is null throw ArgumentNullException.
If
selectoris null throw ArgumentNullException.
If
source has no elements.
If the TSource is a nullable type.
Make the highest value a nullable TSource instance.
Else.
Throw an InvalidOperationException.
Initialize running total: sum as 0, and element counter: count as 0.
Iterate over the
source elements
Get the current value by projecting the current element using the selector function.
If the current element has a value (i.e. Is not null)
sum = previous sum value + current value
count = previous count + 1
Return sum / count.
Note: If an element is null, then it is skipped in the count and does not affect the sum.
EditLoop Count
1 (the source sequence is always iterated one time)
EditCode Samples
Average Operator Unit Tests