IEnumerator.MoveNext 메서드

정의

열거자를 컬렉션의 다음 요소로 진행합니다.

public:
 bool MoveNext();
public bool MoveNext();
abstract member MoveNext : unit -> bool
Public Function MoveNext () As Boolean

반품

true 열거자가 다음 요소로 성공적으로 진행되었으면 이고, false 열거자가 컬렉션의 끝을 통과한 경우

예외

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

예제

다음 코드 예제에서는 사용자 지정 컬렉션에 대 한 인터페이스의 IEnumerator 구현을 보여 줍니다. 이 예제에서는 MoveNext 명시적으로 호출되지 않지만 (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 는 열거자가 컬렉션의 첫 번째 요소 앞에 배치되고 메서드에 대한 MoveNext 첫 번째 호출은 열거자를 컬렉션의 첫 번째 요소 위로 이동합니다.

컬렉션의 끝을 전달하면 MoveNext 열거자가 컬렉션의 마지막 요소 뒤 위치에 배치되고 MoveNext 반환됩니다 false. 열거자가 이 위치에 있으면 호출될 때까지 Reset 반환 falseMoveNext 후속 호출도 있습니다.

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

적용 대상

추가 정보