IEnumerator<T> 介面

定義

支援泛型集合上的簡單反覆運算。

generic <typename T>
public interface class IEnumerator : IDisposable, System::Collections::IEnumerator
public interface IEnumerator<out T> : IDisposable, System.Collections.IEnumerator
public interface IEnumerator<T> : IDisposable, System.Collections.IEnumerator
type IEnumerator<'T> = interface
    interface IEnumerator
    interface IDisposable
type IEnumerator<'T> = interface
    interface IDisposable
    interface IEnumerator
Public Interface IEnumerator(Of Out T)
Implements IDisposable, IEnumerator
Public Interface IEnumerator(Of T)
Implements IDisposable, IEnumerator

類型參數

T

要列舉之物件的類型。

這是共變數的型別參數。 也就是說,您可以使用您指定的類型,或衍生程度較高的任何類型。 如需共變數與反變數的詳細資訊,請參閱泛型中的共變數與反變數
衍生
實作

範例

下列範例顯示 IEnumerator<T> 自訂物件集合類別的 介面實作。 自訂物件是 型 Box 別的實例,而集合類別為 BoxCollection 。 此程式碼範例是介面所提供較大範例的 ICollection<T> 一部分。


// Defines the enumerator for the Boxes collection.
// (Some prefer this class nested in the collection class.)
public class BoxEnumerator : IEnumerator<Box>
{
    private BoxCollection _collection;
    private int curIndex;
    private Box curBox;

    public BoxEnumerator(BoxCollection collection)
    {
        _collection = collection;
        curIndex = -1;
        curBox = default(Box);
    }

    public bool MoveNext()
    {
        //Avoids going beyond the end of the collection.
        if (++curIndex >= _collection.Count)
        {
            return false;
        }
        else
        {
            // Set current box to next item in collection.
            curBox = _collection[curIndex];
        }
        return true;
    }

    public void Reset() { curIndex = -1; }

    void IDisposable.Dispose() { }

    public Box Current
    {
        get { return curBox; }
    }

    object IEnumerator.Current
    {
        get { return Current; }
    }
}
' Defines the enumerator for the Boxes collection.
' (Some prefer this class nested in the collection class.)
Public Class BoxEnumerator
    Implements IEnumerator(Of Box)
    Private _collection As BoxCollection
    Private curIndex As Integer
    Private curBox As Box


    Public Sub New(ByVal collection As BoxCollection)
        MyBase.New()
        _collection = collection
        curIndex = -1
        curBox = Nothing

    End Sub

    Private Property Box As Box
    Public Function MoveNext() As Boolean _
        Implements IEnumerator(Of Box).MoveNext
        curIndex = curIndex + 1
        If curIndex = _collection.Count Then
            ' Avoids going beyond the end of the collection.
            Return False
        Else
            'Set current box to next item in collection.
            curBox = _collection(curIndex)
        End If
        Return True
    End Function

    Public Sub Reset() _
        Implements IEnumerator(Of Box).Reset
        curIndex = -1
    End Sub

    Public Sub Dispose() _
        Implements IEnumerator(Of Box).Dispose

    End Sub

    Public ReadOnly Property Current() As Box _
        Implements IEnumerator(Of Box).Current

        Get
            If curBox Is Nothing Then
                Throw New InvalidOperationException()
            End If

            Return curBox
        End Get
    End Property

    Private ReadOnly Property Current1() As Object _
        Implements IEnumerator.Current

        Get
            Return Me.Current
        End Get
    End Property
End Class

' Defines two boxes as equal if they have the same dimensions.
Public Class BoxSameDimensions
    Inherits EqualityComparer(Of Box)

    Public Overrides Function Equals(ByVal b1 As Box, ByVal b2 As Box) As Boolean
        If b1.Height = b2.Height And b1.Length = b2.Length And b1.Width = b2.Width Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Overrides Function GetHashCode(ByVal bx As Box) As Integer
        Dim hCode As Integer = bx.Height ^ bx.Length ^ bx.Width
        Return hCode.GetHashCode()
    End Function
End Class

備註

IEnumerator<T> 是所有泛型列舉值的基底介面。

foreachC++ 中 C# 語言 (for each 語句, For Each 在 Visual Basic 中) 會隱藏列舉值的複雜度。 因此,建議您使用 foreach,而不要直接使用列舉值。

列舉程式可以用來讀取集合中的資料,但是無法用來修改基礎集合。

一開始,列舉程式位在集合中的第一個項目之前。 在這個位置上,Current 並未定義。 因此,在讀取 MoveNext 的值之前,必須呼叫 Current 以將列舉值前移至集合的第一個項目。

Current 會傳回相同的物件直到呼叫 MoveNextMoveNext 會將 Current 設定為下一個項目。

如果 MoveNext 傳遞集合結尾,列舉值會放在集合的最後一個專案後面,並 MoveNextfalse 回 。 當列舉值位於這個位置時,後續呼叫 MoveNext 也會傳回 false 。 如果最後一 MoveNext 次呼叫傳 false 回 , Current 則為未定義。 您不能再次將 Current 設定為集合的第一個項目;您必須建立新的列舉值執行個體。

系統會 Reset 為 COM 互通性提供 方法。 它不一定需要實作;相反地,實作者可以直接擲回 NotSupportedException 。 不過,如果您選擇這樣做,您應該確定沒有呼叫端依賴 Reset 此功能。

如果對集合進行變更,例如新增、修改或刪除專案,則列舉值的行為是未定義的。

列舉程式沒有集合的獨佔存取權,因此,列舉集合內容本質上並不是安全的執行緒程序。 若要確保列舉期間的執行緒安全性,您可以在整個列舉期間鎖定集合。 若要讓多重執行緒能夠存取集合以便進行讀取和寫入,您必須實作自己的同步處理。

System.Collections.Generic 命名空間中集合的預設實作未同步處理。

給實施者的注意事項

實作此介面需要實作非一般 IEnumerator 介面。 MoveNext()Reset() 方法不相 T 依于 ,而且只會出現在非一般介面上。 屬性 Current 會出現在兩個介面上,而且有不同的傳回型別。 實作非泛型 Current 屬性作為明確的介面實作。 這可讓非泛型介面的任何取用者取用泛型介面。

此外, IEnumerator<T> 實作 IDisposable ,這需要您實作 Dispose() 方法。 這可讓您在使用其他資源時關閉資料庫連結或釋放檔案控制代碼或類似作業。 如果沒有其他資源可處置,請提供空 Dispose() 的實作。

屬性

Current

取得集合中位於列舉值目前位置的元素。

方法

Dispose()

執行與釋放 (Free)、釋放 (Release) 或重設 Unmanaged 資源相關聯之應用程式定義的工作。

(繼承來源 IDisposable)
MoveNext()

將列舉值往前推至下集合中的下一個項目。

(繼承來源 IEnumerator)
Reset()

設定列舉值至它的初始位置,這是在集合中第一個項目之前。

(繼承來源 IEnumerator)

適用於

另請參閱