Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save samueleresca/821318fb8f609072f055 to your computer and use it in GitHub Desktop.
Save samueleresca/821318fb8f609072f055 to your computer and use it in GitHub Desktop.
public static IEnumerable Take(this IEnumerable source, int count)
{
if (source == null) throw Error.ArgumentNull("source");
return TakeIterator(source, count);
}
static IEnumerable TakeIterator(IEnumerable source, int count)
{
if (count > 0)
{
foreach (TSource element in source)
{
yield return element;
if (--count == 0) break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment