My current application has a command hierarchy.
public interface ICommand { void Execute(); }
So, some teams have state, some of them are not.
I need to list IEnumerable in a circular way for some implementation of the command at run time.
public class GetNumberCommand : ICommand { public GetNumberCommand() { List<int> numbers = new List<int> { 1, 2, 3 }; } public void Execute() {
Execute
is called from time to time, so you need to maintain an iterative state.
How to implement this circular enumeration?
I found two solutions:
Using the IEnumerator<T>
Interface It looks like this:
if (!_enumerator.MoveNext()) { _enumerator.Reset(); _enumerator.MoveNext(); }
Using a circular IEnumerable<T>
( yield
forever the same sequence): Implementing a circular iterator .
There may be many more ways to achieve this. What do you recommend to use and why?
source share