// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the Apache 2.0 License. // See the LICENSE file in the project root for more information. using System.Collections.Generic; namespace System.Linq { public static partial class EnumerableEx { #if !(REF_ASSM && NETCOREAPP2_0) /// /// Bypasses a specified number of contiguous elements from the end of the sequence and returns the remaining elements. /// /// Source sequence element type. /// Source sequence. /// /// The number of elements to skip from the end of the sequence before returning the remaining /// elements. /// /// Sequence bypassing the specified number of elements counting from the end of the source sequence. public static IEnumerable SkipLast(this IEnumerable source, int count) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (count < 0) { throw new ArgumentOutOfRangeException(nameof(count)); } return source.SkipLast_(count); } #endif private static IEnumerable SkipLast_(this IEnumerable source, int count) { var q = new Queue(); foreach (var x in source) { q.Enqueue(x); if (q.Count > count) { yield return q.Dequeue(); } } } } }