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.

Current は、MoveNext が呼び出されるまでは同じオブジェクトを返します。Current returns the same object until MoveNext is called. MoveNext は、Current を次の要素に設定します。MoveNext sets Current to the next element.

適用対象

こちらもご覧ください