using System; using System.Collections.Generic; using System.Text; namespace System.Linq { public static partial class Enumerable { // int public static double Average (this IEnumerable source) { if (source == null) throw new ArgumentNullException ("source"); return (double) source.Sum () / source.Count (); } public static double? Average (this IEnumerable source) { if (source == null) throw new ArgumentNullException ("source"); int count = source.Count(); if (count == 0) return null; return (double)source.Sum () / count; } public static double Average (this IEnumerable source, Func selector) { return source.Select (selector).Average (); } public static double? Average (this IEnumerable source, Func selector) { return source.Select (selector).Average (); } // long public static double Average (this IEnumerable source) { if (source == null) throw new ArgumentNullException ("source"); return (double)source.Sum () / source.Count (); } public static double? Average (this IEnumerable source) { if (source == null) throw new ArgumentNullException ("source"); int count = source.Count (); if (count == 0) return null; return (double)source.Sum () / count; } public static double Average (this IEnumerable source, Func selector) { return source.Select (selector).Average (); } public static double? Average (this IEnumerable source, Func selector) { return source.Select (selector).Average (); } // float public static float Average (this IEnumerable source) { if (source == null) throw new ArgumentNullException ("source"); return source.Sum () / source.Count (); } public static float? Average (this IEnumerable source) { if (source == null) throw new ArgumentNullException ("source"); int count = source.Count (); if (count == 0) return null; return source.Sum () / count; } public static float Average (this IEnumerable source, Func selector) { return source.Select (selector).Average (); } public static float? Average (this IEnumerable source, Func selector) { return source.Select (selector).Average (); } // double public static double Average (this IEnumerable source) { if (source == null) throw new ArgumentNullException ("source"); return source.Sum () / source.Count (); } public static double? Average (this IEnumerable source) { if (source == null) throw new ArgumentNullException ("source"); int count = source.Count (); if (count == 0) return null; return source.Sum () / count; } public static double Average (this IEnumerable source, Func selector) { return source.Select (selector).Average (); } public static double? Average (this IEnumerable source, Func selector) { return source.Select (selector).Average (); } // decimal public static decimal Average (this IEnumerable source) { if (source == null) throw new ArgumentNullException ("source"); int count = source.Count (); if (count == 0) ThrowNoElements (); // decimal has no special "NaN" value, so we can't divide by zero. return source.Sum () / count; } public static decimal? Average (this IEnumerable source) { if (source == null) throw new ArgumentNullException ("source"); int count = source.Count (); if (count == 0) return null; return source.Sum () / count; } public static decimal Average (this IEnumerable source, Func selector) { return source.Select (selector).Average (); } public static decimal? Average (this IEnumerable source, Func selector) { return source.Select (selector).Average (); } } }