IEnumerator.Reset Metode

Definisi

Mengatur enumerator ke posisi awalnya, yaitu sebelum elemen pertama dalam koleksi.

public:
 void Reset();
public void Reset ();
abstract member Reset : unit -> unit
Public Sub Reset ()

Pengecualian

Koleksi dimodifikasi setelah enumerator dibuat.

Enumerator tidak mendukung reset.

Contoh

Contoh kode berikut menunjukkan implementasi IEnumerator antarmuka untuk koleksi kustom. Dalam contoh ini, Reset 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

Jika perubahan dilakukan pada koleksi, seperti menambahkan, memodifikasi, atau menghapus elemen, perilaku Reset tidak ditentukan.

Metode Reset ini disediakan untuk interoperabilitas COM. Itu tidak selalu perlu diimplementasikan; sebaliknya, pelaksana cukup melempar NotSupportedException.

Catatan Bagi Implementer

Semua panggilan ke Reset() harus menghasilkan status yang sama untuk enumerator. Implementasi yang disukai adalah memindahkan enumerator ke awal koleksi, sebelum elemen pertama. Ini membatalkan enumerator jika koleksi telah dimodifikasi sejak enumerator dibuat, yang konsisten dengan MoveNext() dan Current.

Berlaku untuk

Lihat juga