Skip to content

Instantly share code, notes, and snippets.

@xepherys
Created September 29, 2020 16:12
Show Gist options
  • Save xepherys/f40c61b368614bec3e735903fe469063 to your computer and use it in GitHub Desktop.
Save xepherys/f40c61b368614bec3e735903fe469063 to your computer and use it in GitHub Desktop.
RollingList Type that will move the indexer back to 0 when the end of the list is reached.
public class RollingList<T> : List<T>
{
private int indexer = 0;
private int maxIndexer;
public RollingList() : base()
{
maxIndexer = this.Count;
}
public RollingList(List<T> list)
{
foreach (var v in list)
this.Add(v);
maxIndexer = this.Count;
}
public new void Add(T item)
{
base.Add(item);
maxIndexer = this.Count;
}
public new void Remove(T item)
{
base.Remove(item);
maxIndexer = this.Count;
}
public T Next()
{
T _ret = this[indexer];
if (indexer + 1 > maxIndexer - 1)
indexer = 0;
else
indexer++;
return _ret;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment