using System.Linq;
using System.Diagnostics;
namespace System.Collections.Generic
{
public static class EnumeratorExtensions
{
///
/// Creates a list by repeating another list.
///
///
/// The list to repeat.
/// The number of items to return.
/// A circular list of items.
public static IEnumerable Cycle(this IEnumerable source, int count)
{
if (source == null)
throw new ArgumentNullException("source");
return CycleIterator(source, count);
}
private static IEnumerable CycleIterator(IEnumerable source, int count)
{
Debug.Assert(source != null, "source is null.");
int i = 0;
while (i < count)
using (IEnumerator data = source.GetEnumerator())
{
while (i < count && data.MoveNext())
{
yield return data.Current;
i++;
}
}
}
}
}