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 、は明示的に呼び出されませんが、(for each Visual Basic) foreachの使用をサポートするように実装されています。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. MoveNextCurrent値を読み取る前に、列挙子をコレクションの最初の要素に進めるには、を呼び出す必要があります。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.

適用対象

こちらもご覧ください