I had a discussion on PHP's InfiniteIterator with a friend.
Is there an equivalent of it in the .NET world?
Not built-in but easy to write:
public static IEnumerable<T> RepeatInfinitely<T>(this IEnumerable<T> sequence)
{
while(true) {
foreach(var item in sequence)
yield return item;
}
}
you can just roll your own.
public class InfiniteEnumerator: System.Collections.IEnumerator
{
private System.Collections.IEnumerator _m_enumerator;
public InfiniteEnumerator(System.Collections.IEnumerator enumerator)
{
_m_enumerator = enumerator;
}
public object Current
{
get { return _m_enumerator.Current; }
}
public bool MoveNext()
{
if (!_m_enumerator.MoveNext())
{
_m_enumerator.Reset();
return _m_enumerator.MoveNext();
}
else
{
return true;
}
}
public void Reset()
{
_m_enumerator.Reset();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With