IEnumerator.Current IEnumerator.Current IEnumerator.Current IEnumerator.Current Property

정의

컬렉션에서 열거자의 현재 위치에 있는 요소를 가져옵니다.Gets the element in the collection at the current position of the enumerator.

public:
 property System::Object ^ Current { System::Object ^ get(); };
public object Current { get; }
member this.Current : obj
Public ReadOnly Property Current As Object

속성 값

컬렉션에서 열거자의 현재 위치에 있는 요소입니다.The element in the collection at the current position of the enumerator.

예제

다음 코드 예제에서는 구현의 IEnumerator 사용자 지정 컬렉션에 대 한 인터페이스입니다.The following code example demonstrates the implementation of the IEnumerator interfaces for a custom collection. 이 예에서 Current 명시적으로 호출 되지 않습니다 하지만의 사용을 지원 하도록 구현 됩니다 foreach (for each Visual basic에서).In this example, Current is not explicitly called, but it is implemented to support the use of foreach (for each in Visual Basic). 이 코드 예제는에 대 한 보다 큰 예제의 일부는 IEnumerator 인터페이스입니다.This code example is part of a larger example for the IEnumerator interface.

// 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

설명

Current 다음 조건 중 하나라도 아래 정의 되어 있지 않습니다.Current is undefined under any of the following conditions:

  • 열거자가 만들어진 후에 즉시 열거자는 컬렉션의 첫 번째 요소 앞 배치 됩니다.The enumerator is positioned before the first element in the collection, immediately after the enumerator is created. MoveNext 호출 하 여 열거자의 값을 읽기 전에 컬렉션의 첫 번째 요소를 Current입니다.MoveNext must be called to advance the enumerator to the first element of the collection before reading the value of Current.

  • 마지막으로 호출한 MoveNext 반환 false, 컬렉션의 끝을 나타내는입니다.The last call to MoveNext returned false, which indicates the end of the collection.

  • 요소의 추가, 수정 또는 삭제 하는 등 컬렉션에서 변경 되어 열거자가 무효화 됩니다.The enumerator is invalidated due to changes made in the collection, such as adding, modifying, or deleting elements.

CurrentMoveNext가 호출될 때까지 동일한 개체를 반환합니다.Current returns the same object until MoveNext is called. MoveNextCurrent를 다음 요소로 설정합니다.MoveNext sets Current to the next element.

적용 대상

추가 정보