Type to track the position of a list using IEnumerable
<p>I’m trying to develop a type to track the current iteration position with a list.</p>

<p>I ideally want to used it with a foreach loop using the IEnumerable interface but the interface has no start/stop events or method to hooking to reset the count. </p>

<p>Currently I have created a GetNext( ) method which return the next value in the list and increments a count by 1.</p>

<p>Does anyone know I can achieve the same functionality using IEnumerable so I can use the type with a foreach loop?</p>

<p>So for example; imagine a list contains 10 items. One method could iterate an instance of the type to position 4 then method two would iterate the same instance starting at position 5 to 6 then method 3 would iterate the remainng from position 7  to 10 – so the type instance tracks the current position.</p>

<p>Any ideas are greatly appreciated (code shown below) . Thanks</p>

<pre><code>public sealed class PositionTracker&lt;T&gt; : IEnumerable
{
    private readonly object _syncLock = new object();
    private readonly IList&lt;T&gt; _list = new List&lt;T&gt;();
    private int _current;

    public PositionTracker(IList&lt;T&gt; list)
    {
        _list = list;
    }

    public T GetCurrent()
    {
        lock (_syncLock)
        {
            return _list[_current];
        }
    }

    public T GetNext()
    {
        lock (_syncLock)
        {
            T t = GetCurrent();
            if (_current &lt; _list.Count - 1)
            {
                _current++;
            }
            return t;
        }
    } 

    public IEnumerator&lt;T&gt; GetEnumerator()
    {
        lock (_syncLock)
        {
            return _list.GetEnumerator();
        }
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

    public void Reset()
    {
        lock (_syncLock)
        {
            _current = 0;
        }
    }

    public int Count
    {
        get
        {
            lock (_syncLock)
            {
                return _list.Count;
            }
        }
    }
}

[TestFixture]
public class PositionTrackerTests
{
    [Test]
    public void Position_CurrentPosition_Test()
    {
        List&lt;string&gt; list = new List&lt;string&gt;(new string[] { "A", "B", "C", "D" });
        PositionTracker&lt;string&gt; positionTracker = new PositionTracker&lt;string&gt;(list);

        Assert.IsTrue(positionTracker.GetNext().Equals("A"));
        Assert.IsTrue(positionTracker.GetNext().Equals("B"));
        Assert.IsTrue(positionTracker.GetNext().Equals("C"));
        Assert.IsTrue(positionTracker.GetNext().Equals("D"));
    }
}
</code></pre>
