How Can I Use IEnumerator.Reset()?

前端 未结 3 1177
春和景丽
春和景丽 2020-12-05 13:15

How exactly is the right way to call IEnumerator.Reset?

The documentation says:

The Reset method is provided for COM interoperabi

3条回答
  •  天命终不由人
    2020-12-05 13:53

    public class PeopleEnum : IEnumerator
    {
        public Person[] _people;
    
        // Enumerators are positioned before the first element 
        // until the first MoveNext() call. 
        int position = -1;
    
        public PeopleEnum(Person[] list)
        {
            _people = list;
        }
    
        public bool MoveNext()
        {
            position++;
            return (position < _people.Length);
        }
    
        public void Reset()
        {
            position = -1;
        }
    
        object IEnumerator.Current
        {
            get
            {
                return Current;
            }
        }
    
        public Person Current
        {
            get
            {
                try
                {
                    return _people[position];
                }
                catch (IndexOutOfRangeException)
                {
                    throw new InvalidOperationException();
                }
            }
        }
    }
    

提交回复
热议问题