Hooked on LINQ

Hooked on LINQ - Developers' Wiki
for .NET Language Integrated Query

Companion book for this site
LINQ to Objects Using C# 4.0:
Using and Extending LINQ to Objects and Parallel LINQ (PLINQ)
Quick Search

Advanced Search »

Average Operator

Modified: 2007/01/22 22:51 by t_magennis - Categorized as: LINQ to Objects
{TOC}
Namespace:System.Linq
Assembly:System.Core.dll
Extends:IEnumerable<T>

Back to Standard Query Operator Index


Edit

Introduction

The Average operator computes the average of a sequence of numeric values.

Edit

Method Signatures

// 1 - Average all elements public static decimal Average( this IEnumerable source)

public static double Average( this IEnumerable source)

public static Nullable Average( this IEnumerable> source)

public static Nullable Average( this IEnumerable> source)

public static double Average( this IEnumerable source)

public static Nullable Average( this IEnumerable> source)

public static double Average( this IEnumerable source)

public static Nullable Average( this IEnumerable> source)

public static Nullable Average( this IEnumerable> source)

public static float Average( this IEnumerable source)

// 2 - Average all elements derived from a selector function. public static decimal Average( this IEnumerable source, Func selector)

public static double Average( this IEnumerable source, Func selector)

public static double Average( this IEnumerable source, Func selector)

public static double Average( this IEnumerable source, Func selector)

public static Nullable Average( this IEnumerable source, Func> selector)

public static Nullable Average( this IEnumerable source, Func> selector)

public static Nullable Average( this IEnumerable source, Func> selector)

public static Nullable Average( this IEnumerable source, Func> selector)

public static Nullable Average( this IEnumerable source, Func> selector)

public static float Average( this IEnumerable source, Func selector)

Edit

Exceptions

Throws an ArgumentNullException if source or selector is null.
Throws an InvalidOperationException if there are no elements in source.


Edit

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


Edit

Loop Count

1 (the source sequence is always iterated one time)


Edit

Code Samples

Average Operator Unit Tests

If you would like to comment on this page, click on the Discuss button located on the top-right of each page. Feel free to edit any mistakes or omissions you find. If you have an objection or find in-appropriate content then contact the administrator. This website is not affiliated with Microsoft®, all content and opinions are those of the specific author and some advice, solutions and article may contain unintentional errors - please use care. Other websites by this author: Focused Objective, Geek Speak Decoded.