IEnumerator.Current Properti

Definisi

Mendapatkan elemen dalam koleksi pada posisi enumerator saat ini.

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

Nilai Properti

Elemen dalam koleksi pada posisi enumerator saat ini.

Contoh

Contoh kode berikut menunjukkan implementasi IEnumerator antarmuka untuk koleksi kustom. Dalam contoh ini, Current tidak secara eksplisit disebut, tetapi diimplementasikan untuk mendukung penggunaan foreach (for each di Visual Basic). Contoh kode ini adalah bagian dari contoh yang lebih besar untuk IEnumerator antarmuka.

// 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

Keterangan

Current tidak ditentukan dalam salah satu kondisi berikut:

  • Enumerator diposisikan sebelum elemen pertama dalam koleksi, segera setelah enumerator dibuat. MoveNext harus dipanggil untuk memajukan enumerator ke elemen pertama koleksi sebelum membaca nilai Current.

  • Panggilan terakhir untuk MoveNext dikembalikan false, yang menunjukkan akhir koleksi.

  • Enumerator tidak valid karena perubahan yang dibuat dalam koleksi, seperti menambahkan, memodifikasi, atau menghapus elemen.

Current mengembalikan objek yang sama hingga MoveNext dipanggil. MoveNextCurrent diatur ke elemen berikutnya.

Berlaku untuk

Lihat juga