IEnumerator.Reset 메서드

정의

컬렉션의 첫 번째 요소 앞의 초기 위치에 열거자를 설정합니다.

public:
 void Reset();
public void Reset ();
abstract member Reset : unit -> unit
Public Sub Reset ()

예외

열거자가 만들어진 후에 컬렉션이 수정되었습니다.

예제

다음 코드 예제에서는 사용자 지정 컬렉션에 IEnumerator 대 한 인터페이스의 구현을 보여 줍니다. 이 예제에서 는 Reset 명시적으로 호출되지 않지만 ( Visual Basic의 경우)for eachforeach 사용을 지원하기 위해 구현됩니다. 이 코드 예제는 인터페이스에 대한 더 큰 예제의 IEnumerator 일부입니다.

// When you implement IEnumerable, you must also implement IEnumerator.
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();
            }
        }
    }
}
' When you implement IEnumerable, you must also implement IEnumerator.
Public Class PeopleEnum
    Implements IEnumerator

    Public _people() As Person

    ' Enumerators are positioned before the first element
    ' until the first MoveNext() call.
    Dim position As Integer = -1

    Public Sub New(ByVal list() As Person)
        _people = list
    End Sub

    Public Function MoveNext() As Boolean Implements IEnumerator.MoveNext
        position = position + 1
        Return (position < _people.Length)
    End Function

    Public Sub Reset() Implements IEnumerator.Reset
        position = -1
    End Sub

    Public ReadOnly Property Current() As Object Implements IEnumerator.Current
        Get
            Try
                Return _people(position)
            Catch ex As IndexOutOfRangeException
                Throw New InvalidOperationException()
            End Try
        End Get
    End Property
End Class

설명

요소 추가, 수정 또는 삭제와 같이 컬렉션이 변경되면 의 Reset 동작이 정의되지 않습니다.

Reset COM 상호 운용성을 위해 메서드가 제공됩니다. 반드시 구현할 필요는 없습니다. 대신 구현자는 단순히 을 throw할 NotSupportedException수 있습니다.

구현자 참고

에 대 한 모든 호출 Reset() 열거자에 대 한 동일한 상태의 결과 여야 합니다. 기본 구현은 첫 번째 요소 앞 컬렉션의 시작 부분에 열거자를 이동 합니다. 열거자가 무효화이 컬렉션에 부합 되는 열거자가 만들어진 이후 수정 되었는지 MoveNext()Current입니다.

적용 대상

추가 정보