IEnumerator.Current 속성

정의

컬렉션에서 열거자의 현재 위치에 있는 요소를 가져옵니다.

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

속성 값

컬렉션에서 열거자의 현재 위치에 있는 요소입니다.

예제

다음 코드 예제에서는 사용자 지정 컬렉션에 IEnumerator 대 한 인터페이스의 구현을 보여 줍니다. 이 예제에서는 가 Current 명시적으로 호출되지 않지만 ( 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

설명

Current 는 다음 조건 중에서 정의되지 않습니다.

  • 열거자는 열거자를 만든 직후 컬렉션의 첫 번째 요소 앞에 배치됩니다. MoveNext 값을 Current읽기 전에 열거자를 컬렉션의 첫 번째 요소로 진행하려면 를 호출해야 합니다.

  • 마지막으로 호출한 MoveNext 반환 false, 컬렉션의 끝을 나타내는입니다.

  • 요소의 추가, 수정 또는 삭제 하는 등 컬렉션에서 변경 되어 열거자가 무효화 됩니다.

CurrentMoveNext가 호출될 때까지 동일한 개체를 반환합니다. MoveNextCurrent를 다음 요소로 설정합니다.

적용 대상

추가 정보