public static IEnumerable <T> Repeat <T>(T element, int count) { if (count < 0) { throw EnumerableExtra.CreateOutOfRange("count"); } return(RepeatHelper(element, count)); }
public static IEnumerable <int> Range(int start, int count) { long test = (start + (long)count) - 1L; if (count < 0 || test > (long)Int32.MaxValue) { throw EnumerableExtra.CreateOutOfRange("count"); } return(RangeHelper(start, count)); }
public static T First <T>(this IEnumerable <T> enumerable) { if (enumerable == null) { throw new ArgumentNullException("enumerable"); } using (var e = enumerable.GetEnumerator()) { if (!e.MoveNext()) { throw EnumerableExtra.CreateEmpty(); } return(e.Current); } }
private static T AggregateHelper <T>(this IEnumerable <T> source, Func <T, T, T> func) { using (var e = source.GetEnumerator()) { if (!e.MoveNext()) { throw EnumerableExtra.CreateEmpty(); } var result = e.Current; while (e.MoveNext()) { result = func(result, e.Current); } return(result); } }
public static double Max(this IEnumerable <double> e) { if (e == null) { throw new ArgumentNullException("e"); } using (var enumerator = e.GetEnumerator()) { if (!enumerator.MoveNext()) { throw EnumerableExtra.CreateEmpty(); } var max = enumerator.Current; while (enumerator.MoveNext()) { if (enumerator.Current > max) { max = enumerator.Current; } } return(max); } }
public static long Min(this IEnumerable <long> e) { if (e == null) { throw new ArgumentNullException("e"); } using (var enumerator = e.GetEnumerator()) { if (!enumerator.MoveNext()) { throw EnumerableExtra.CreateEmpty(); } var min = enumerator.Current; while (enumerator.MoveNext()) { if (enumerator.Current < min) { min = enumerator.Current; } } return(min); } }
public static T ElementAt <T>(this IEnumerable <T> enumerable, int count) { if (enumerable == null) { throw new ArgumentNullException("enumerable"); } using (var e = enumerable.GetEnumerator()) { int cur = 0; do { if (!e.MoveNext()) { throw EnumerableExtra.CreateOutOfRange("count"); } cur++; }while (cur <= count); return(e.Current); } }
public static T First <T>(this IEnumerable <T> enumerable, Func <T, bool> predicate) { if (enumerable == null) { throw new ArgumentNullException("enumerable"); } if (predicate == null) { throw new ArgumentNullException("predicate"); } foreach (var cur in enumerable) { if (predicate(cur)) { return(cur); } } throw EnumerableExtra.CreateNoMatch(); }