IEnumerator 接口
public interface IEnumerator { bool MoveNext(); object Current { get; } void Reset(); }实现IEnumerable接口的话需要实现其 IEnumerator.GetEnumerator 方法,其后也是需要实现 IEnumerator接口的方法(迭代模板)
public class ListEnumerator : IEnumerator { private int _currentIndex = -1; public bool MoveNext() { _currentIndex++; return (_currentIndex < _objects.Count); } public object Current { get { try { return _objects[_currentIndex]; } catch (IndexOutOfRangeException) { throw new InvalidOperationException(); } } public void Reset() { _currentIndex = -1; } }
转载于:https://www.cnblogs.com/ILoveMyJob/p/10786808.html