diff --git a/MoreLinq/Slice.cs b/MoreLinq/Slice.cs index 86f8306fa..656be64f3 100644 --- a/MoreLinq/Slice.cs +++ b/MoreLinq/Slice.cs @@ -19,7 +19,6 @@ namespace MoreLinq { using System; using System.Collections.Generic; - using System.Linq; public static partial class MoreEnumerable { @@ -57,9 +56,22 @@ public static IEnumerable Slice(this IEnumerable sequence, int startInd { IList list => SliceList(list.Count, i => list[i]), IReadOnlyList list => SliceList(list.Count, i => list[i]), - var seq => seq.Skip(startIndex).Take(count) + _ => SliceDefault() }; + IEnumerable SliceDefault() + { + var lastIndex = startIndex + count; + var index = -1; + foreach (var item in sequence) + { + if (++index >= lastIndex) + yield break; + else if (index >= startIndex) + yield return item; + } + } + IEnumerable SliceList(int listCount, Func indexer) { var countdown = count;