Created
January 9, 2018 22:08
-
-
Save JohanLarsson/444be02a07c845b087d4c2444f8757d8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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