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

プロパティ値

Object

コレクション内の、列挙子の現在位置にある要素。

次のコード例は、カスタム コレクションのインターフェイスの IEnumerator 実装を示しています。 この例では、Current明示的に呼び出されませんが、(for eachVisual Basic) のforeach使用をサポートするように実装されています。 このコード例は、インターフェイスのより大きな例の 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を読み取る前に、列挙子をコレクションの最初の要素に進めるために呼び出す必要があります。

  • 返されるfalse最後の呼び出しMoveNext。これは、コレクションの末尾を示します。

  • 列挙子は、要素の追加、変更、削除など、コレクション内で行われた変更により無効になります。

Current は、MoveNext が呼び出されるまでは同じオブジェクトを返します。 MoveNext は、Current を次の要素に進めます。

適用対象

こちらもご覧ください