IEnumerator.Reset 메서드

정의

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

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

예외

열거자를 만든 후 컬렉션이 수정되었습니다.

열거자는 재설정을 지원하지 않습니다.

예제

다음 코드 예제에서는 사용자 지정 컬렉션에 대 한 인터페이스의 IEnumerator 구현을 보여 줍니다. 이 예제에서는 Reset 명시적으로 호출되지 않지만 (Visual Basic에서)foreach 사용을 for each 지원하도록 구현됩니다. 이 코드 예제는 인터페이스에 대한 더 큰 예제의 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 이 정의되지 않습니다.

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

구현자 참고

모든 호출은 Reset() 열거자에 대해 동일한 상태를 발생시켜야 합니다. 기본 구현은 첫 번째 요소 앞에 있는 컬렉션의 시작 부분으로 열거자를 이동하는 것입니다. 이렇게 하면 열거자가 생성된 이후 컬렉션이 수정된 경우 열거자가 무효화됩니다. 이는 일치 MoveNext() 하며 일치합니다 Current.

적용 대상

추가 정보