Skip to content

Instantly share code, notes, and snippets.

@JohanLarsson
Created January 9, 2018 22:08
Show Gist options
  • Save JohanLarsson/444be02a07c845b087d4c2444f8757d8 to your computer and use it in GitHub Desktop.
Save JohanLarsson/444be02a07c845b087d4c2444f8757d8 to your computer and use it in GitHub Desktop.
public static class EnumerableExt
{
public static IEnumerable<T> SkipLast<T>(this IEnumerable<T> source)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
return SkipLastCore();
IEnumerable<T> SkipLastCore()
{
using (var iterator = source.GetEnumerator())
{
if(!iterator.MoveNext())
{
yield break;
}
var previous = iterator.Current;
while (iterator.MoveNext())
{
yield return previous;
previous = iterator.Current;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment