public void TestEmptyRemainerMoveNextReturnedFalse() { IEnumerable <int> sequence = new int[] { 0 }; IEnumerator <int> enumerator = new DisposableEnumerator(sequence.GetEnumerator()); enumerator.MoveNext(); enumerator.MoveNext(); var result = enumerator.ToEnumerable(includeCurrent: null); var expected = sequence.Skip(1); Assert.IsTrue(expected.SequenceEqual(result)); }
/// <summary> /// Returns the sequence skipping the specified index. /// </summary> /// <exception cref="ArgumentNullException"/> /// <exception cref="ArgumentOutOfRangeException"/> public static IEnumerable SkipIndex(this IEnumerable source, int index) { Error.ThrowIfNull(source, nameof(source)); Error.ThrowIfBelowRange(index, nameof(index), 0); int currentIndex = -1; bool skipped = false; using DisposableEnumerator enumerator = new DisposableEnumerator(source.GetEnumerator()); while (enumerator.MoveNext()) { ++currentIndex; if (currentIndex != index) { yield return(enumerator.Current); } else { skipped = true; } } if (!skipped) { Error.ThrowArgumentOutOfRange(nameof(index)); } }
/// <summary> /// Returns the specified collection while inserting the collection specified starting from the specified index. /// </summary> /// <param name="items">the collection to be inserted</param> /// <exception cref="ArgumentNullException"/> /// <exception cref="ArgumentOutOfRangeException"/> /// <exception cref="InvalidOperationException"/> public static IEnumerable EnumerableInsert(this IEnumerable source, int index, IEnumerable items) { Error.ThrowIfNull(source, nameof(source)); Error.ThrowIfBelowRange(index, nameof(index), 0); int counter = 0; using (DisposableEnumerator enumerator = new DisposableEnumerator(source.GetEnumerator())) { while (enumerator.MoveNext()) { if (counter == index) { foreach (object item in items) { yield return(item); } } yield return(enumerator.Current); ++counter; } } if (counter == index) { foreach (object item in items) { yield return(item); } } else if (counter < index) { Error.ThrowArgumentOutOfRange(nameof(index)); } }
/// <summary> /// Returns the specified collection while inserting the item specified at the specified index. /// </summary> /// <param name="item">the item to be inserted</param> /// <exception cref="ArgumentNullException"/> /// <exception cref="ArgumentOutOfRangeException"/> /// <exception cref="InvalidOperationException"/> public static IEnumerable EnumerableInsert(this IEnumerable source, int index, object item) { Error.ThrowIfNull(source, nameof(source)); Error.ThrowIfBelowRange(index, nameof(index), 0); int counter = 0; using (DisposableEnumerator enumerator = new DisposableEnumerator(source.GetEnumerator())) { while (enumerator.MoveNext()) { if (counter == index) { yield return(item); } yield return(enumerator.Current); ++counter; } } if (counter == index) //Insert to the end - same as EnumerableAdd { yield return(item); } else if (counter < index) //Index was greater than the last index if the item was to be added to the end { Error.ThrowArgumentOutOfRange(nameof(index)); } }
public static object FirstOrNull(this IEnumerable source) { if (source == null) { throw new ArgumentNullException("source"); } IList collection = source as IList; if (collection != null) { if (collection.Count > 0) { return(collection[0]); } } else { using (DisposableEnumerator enumerator = source.GetDisposableEnumerator()) { if (enumerator.MoveNext()) { return(enumerator.Current); } } } return(null); }
public static object First(this IEnumerable source) { if (source == null) { throw new ArgumentNullException("source"); } IList collection = source as IList; if (collection != null) { if (collection.Count > 0) { return(collection[0]); } } else { using (DisposableEnumerator enumerator = source.GetDisposableEnumerator()) { if (enumerator.MoveNext()) { return(enumerator.Current); } } } throw new InvalidOperationException("Sequence contains no elements"); }
public static bool Any(this IEnumerable source) { Error.ThrowIfNull(source, nameof(source)); using DisposableEnumerator e = new DisposableEnumerator(source.GetEnumerator()); if (e.MoveNext()) { return(true); } return(false); }
public static long LongCount(this IEnumerable source) { Error.ThrowIfNull(source, nameof(source)); long count = 0; using DisposableEnumerator e = new DisposableEnumerator(source.GetEnumerator()); checked { while (e.MoveNext()) { ++count; } } return(count); }
public static bool Any(this IEnumerable source) { if (source == null) { throw new ArgumentNullException("source"); } using (DisposableEnumerator enumerator = source.GetDisposableEnumerator()) { if (enumerator.MoveNext()) { return(true); } } return(false); }
public static int Count(this IEnumerable source) { Error.ThrowIfNull(source, nameof(source)); if (source is ICollection collection) { return(collection.Count); } int count = 0; using DisposableEnumerator e = new DisposableEnumerator(source.GetEnumerator()); checked { while (e.MoveNext()) { ++count; } } return(count); }