IEnumerator.Current プロパティ

定義

列挙子の現在位置にあるコレクション内の要素を取得します。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

プロパティ値

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 は、は明示的に呼び出されませんが、(Visual Basic) の使用をサポートするように実装されてい foreach for each ます。In this example, Current is not explicitly called, but it is implemented to support the use of foreach (for each in Visual Basic). このコード例は、インターフェイスのより大きな例の一部です IEnumeratorThis 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.

適用対象

こちらもご覧ください