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> 是所有泛型枚举器的基本接口。

foreach Visual Basic) for each C++ For Each 中的 C# 语言 (语句隐藏枚举器的复杂性。 因此,建议使用 foreach,而不是直接操作枚举数。

枚举器可用于读取集合中的数据,但不能用于修改基础集合。

最初,枚举数定位在集合中第一个元素的前面。 在此位置上,未定义 Current。 因此,在读取 MoveNext 的值之前,必须调用 Current 将枚举器向前移动到集合的第一个元素。

在调用 Current 之前,MoveNext 返回相同的对象。 MoveNextCurrent 设置为下一个元素。

如果 MoveNext 传递集合的末尾,则枚举器位于集合中最后一个元素之后,并 MoveNext 返回 false。 当枚举器位于此位置时,对 MoveNext 的后续调用也会返回 false。 如果最后一次MoveNext调用返回 ,falseCurrent则为未定义。 无法再次将 Current 设置为集合的第一个元素;必须改为创建新的枚举器实例。

提供 Reset 方法是为了实现 COM 互操作性。 它不一定需要实施:相反,实现者只需引发 。NotSupportedException 但是,如果选择执行此操作,则应确保没有调用方依赖该功能 Reset

如果对集合进行了更改,例如添加、修改或删除元素,则枚举器的行为是不确定的。

枚举数没有对集合的独占访问权;因此,从头到尾对一个集合进行枚举在本质上不是一个线程安全的过程。 若要确保枚举过程中的线程安全性,可以在整个枚举过程中锁定集合。 若要允许多个线程访问集合以进行读写操作,则必须实现自己的同步。

System.Collections.Generic 命名空间中集合的默认实现是不同步的。

实施者说明

实现此接口需要实现非泛型 IEnumerator 接口。 MoveNext()Reset() 方法不依赖于 T,并且仅出现在非泛型接口上。 属性 Current 显示在两个接口上,并具有不同的返回类型。 实现非泛型 Current 属性作为显式接口实现。 这允许非泛型接口的任何使用者使用泛型接口。

此外, IEnumerator<T> 实现 IDisposable,这要求实现 Dispose() 方法。 这使你可以在使用其他资源时关闭数据库连接或释放文件句柄或类似操作。 如果没有要释放的其他资源,请提供一个空 Dispose() 的实现。

属性

Current

获取集合中位于枚举数当前位置的元素。

方法

Dispose()

执行与释放或重置非托管资源关联的应用程序定义的任务。

(继承自 IDisposable)
MoveNext()

将枚举数推进到集合的下一个元素。

(继承自 IEnumerator)
Reset()

将枚举数设置为其初始位置,该位置位于集合中第一个元素之前。

(继承自 IEnumerator)

适用于

另请参阅