更新:2007 年 11 月
返回对枚举数对象的引用,用于对 Collection 对象 (Visual Basic) 进行迭代。
Public Function GetEnumerator() As IEnumerator
备注
For Each...Next 语句 (Visual Basic) 调用 GetEnumerator 来获取枚举数对象,以支持对集合的元素进行迭代。通常,您会使用 For Each...Next 循环遍历一个集合或数组,而不需要显式调用 GetEnumerator。
如果需要对迭代进行比 For Each...Next 语句更紧密的控制,您可以使用 GetEnumerator 方法执行自定义遍历。以下是您可能需要这样操作的几种情况。
- 您可能要返回到集合的开始,并在完成它之前重新开始迭代。 
- 您可能会因为各种原因而跳过一个或多个元素。 
- 可能需要在遍历的过程中更改集合的元素。这时您必须获取一个新的枚举数对象,因为以前的已无效。 
示例
下面的示例演示如何使用 GetEnumerator 检索 Collection 对象的所有元素。
Dim customers As New Collection
' Insert code to add elements to the customers collection.
Dim custEnum As IEnumerator = customers.GetEnumerator()
custEnum.Reset()
Dim thisCustomer As Object
While custEnum.MoveNext()
    thisCustomer = custEnum.Current()
    ' Insert code to process this element of the collection.
End While
GetEnumerator 构造并返回枚举数对象,该对象实现 System.Collections 命名空间的 IEnumerator 接口。该枚举数对象公开 Current 属性以及 MoveNext 和 Reset 方法。有关更多信息,请参见 For Each...Next 语句 (Visual Basic)。
要求
**模块:**Collection
程序集: Visual Basic 运行库(在 Microsoft.VisualBasic.dll 中)