IEnumerator.MoveNext 메서드

정의

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

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

반환

열거자가 다음 요소로 이동한 경우 true이(가) 반환되고, 컬렉션의 끝을 지난 경우 false이(가) 반환됩니다.

예외

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

예제

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

경우 MoveNext 열거자를 컬렉션의 끝 컬렉션의 마지막 요소 뒤에 배치 되는 전달 하 고 MoveNext 반환 false합니다. 열거자가 있는 경우이 위치에 대 한 후속 호출은 MoveNext 반환할 수도 false 때까지 Reset 라고 합니다.

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

적용 대상

추가 정보