using System; namespace MemoryPools.Collections.Linq { public static partial class PoolingEnumerable { public static int Sum(this IPoolingEnumerable source) { if (source == null) throw new ArgumentNullException(nameof(source)); int sum = 0; checked { foreach (var v in source) sum += v; } return sum; } public static int? Sum(this IPoolingEnumerable source) { if (source == null) throw new ArgumentNullException(nameof(source)); int sum = 0; checked { foreach (var v in source) { if (v != null) sum += v.GetValueOrDefault(); } } return sum; } public static long Sum(this IPoolingEnumerable source) { if (source == null) throw new ArgumentNullException(nameof(source)); long sum = 0; checked { foreach (long v in source) sum += v; } return sum; } public static long? Sum(this IPoolingEnumerable source) { if (source == null) throw new ArgumentNullException(nameof(source)); long sum = 0; checked { foreach (var v in source) { if (v != null) sum += v.GetValueOrDefault(); } } return sum; } public static float Sum(this IPoolingEnumerable source) { if (source == null) throw new ArgumentNullException(nameof(source)); double sum = 0; foreach (var v in source) sum += v; return (float)sum; } public static float? Sum(this IPoolingEnumerable source) { if (source == null) throw new ArgumentNullException(nameof(source)); double sum = 0; foreach (var v in source) { if (v != null) sum += v.GetValueOrDefault(); } return (float)sum; } public static double Sum(this IPoolingEnumerable source) { if (source == null) throw new ArgumentNullException(nameof(source)); double sum = 0; foreach (var v in source) sum += v; return sum; } public static double? Sum(this IPoolingEnumerable source) { if (source == null) throw new ArgumentNullException(nameof(source)); double sum = 0; foreach (var v in source) { if (v != null) sum += v.GetValueOrDefault(); } return sum; } public static decimal Sum(this IPoolingEnumerable source) { if (source == null) throw new ArgumentNullException(nameof(source)); decimal sum = 0; foreach (var v in source) sum += v; return sum; } public static decimal? Sum(this IPoolingEnumerable source) { if (source == null) throw new ArgumentNullException(nameof(source)); decimal sum = 0; foreach (var v in source) { if (v != null) sum += v.GetValueOrDefault(); } return sum; } public static int Sum(this IPoolingEnumerable source, Func selector) => Sum(source.Select(selector)); public static int? Sum(this IPoolingEnumerable source, Func selector) => Sum(source.Select(selector)); public static long Sum(this IPoolingEnumerable source, Func selector) => Sum(source.Select(selector)); public static long? Sum(this IPoolingEnumerable source, Func selector) => Sum(source.Select(selector)); public static float Sum(this IPoolingEnumerable source, Func selector) => Sum(source.Select(selector)); public static float? Sum(this IPoolingEnumerable source, Func selector) => Sum(source.Select(selector)); public static double Sum(this IPoolingEnumerable source, Func selector) => Sum(source.Select(selector)); public static double? Sum(this IPoolingEnumerable source, Func selector) => Sum(source.Select(selector)); public static decimal Sum(this IPoolingEnumerable source, Func selector) => Sum(source.Select(selector)); public static decimal? Sum(this IPoolingEnumerable source, Func selector) => Sum(source.Select(selector)); } }