Array.GetEnumerator 方法

定义

返回 IEnumeratorArray

public:
 virtual System::Collections::IEnumerator ^ GetEnumerator();
public System.Collections.IEnumerator GetEnumerator ();
public virtual System.Collections.IEnumerator GetEnumerator ();
abstract member GetEnumerator : unit -> System.Collections.IEnumerator
override this.GetEnumerator : unit -> System.Collections.IEnumerator
Public Function GetEnumerator () As IEnumerator
Public Overridable Function GetEnumerator () As IEnumerator

返回

IEnumerator

IEnumerator 的一个 Array

实现

示例

下面的代码示例演示如何用于 GetEnumerator 列出数组的元素。

using namespace System;

int main()
{
   // Creates and initializes a new Array.
   array<String^>^myArr = gcnew array<String^>(10);
   myArr[ 0 ] = "The";
   myArr[ 1 ] = "quick";
   myArr[ 2 ] = "brown";
   myArr[ 3 ] = "fox";
   myArr[ 4 ] = "jumps";
   myArr[ 5 ] = "over";
   myArr[ 6 ] = "the";
   myArr[ 7 ] = "lazy";
   myArr[ 8 ] = "dog";
   
   // Displays the values of the Array.
   int i = 0;
   System::Collections::IEnumerator^ myEnumerator = myArr->GetEnumerator();
   Console::WriteLine( "The Array contains the following values:" );
   while ( (myEnumerator->MoveNext()) && (myEnumerator->Current != nullptr) )
      Console::WriteLine( "[{0}] {1}", i++, myEnumerator->Current );
}

/* 
This code produces the following output.

The Array contains the following values:
[0] The
[1] quick
[2] brown
[3] fox
[4] jumps
[5] over
[6] the
[7] lazy
[8] dog

*/
using System;

public class SamplesArray  {

   public static void Main()  {

      // Creates and initializes a new Array.
      String[] myArr = new String[10];
      myArr[0] = "The";
      myArr[1] = "quick";
      myArr[2] = "brown";
      myArr[3] = "fox";
      myArr[4] = "jumps";
      myArr[5] = "over";
      myArr[6] = "the";
      myArr[7] = "lazy";
      myArr[8] = "dog";

      // Displays the values of the Array.
      int i = 0;
      System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
      Console.WriteLine( "The Array contains the following values:" );
      while (( myEnumerator.MoveNext() ) && ( myEnumerator.Current != null ))
         Console.WriteLine( "[{0}] {1}", i++, myEnumerator.Current );
   }
}


/*
This code produces the following output.

The Array contains the following values:
[0] The
[1] quick
[2] brown
[3] fox
[4] jumps
[5] over
[6] the
[7] lazy
[8] dog

*/
// Creates and initializes a new Array.
let myArr = Array.zeroCreate 10
myArr[0..8] <- 
   [| "The"
      "quick"
      "brown"
      "fox"
      "jumps"
      "over"
      "the"
      "lazy"
      "dog" |]

// Displays the values of the Array.
let mutable i = 0
let myEnumerator = myArr.GetEnumerator()
printfn "The Array contains the following values:"

while myEnumerator.MoveNext() && myEnumerator.Current <> null do
      printfn $"[{i}] {myEnumerator.Current}" 
      i <- i + 1


// This code produces the following output.
//     The Array contains the following values:
//     [0] The
//     [1] quick
//     [2] brown
//     [3] fox
//     [4] jumps
//     [5] over
//     [6] the
//     [7] lazy
//     [8] dog
Public Class SamplesArray

   Public Shared Sub Main()

      ' Creates and initializes a new Array.
      Dim myArr(10) As [String]
      myArr(0) = "The"
      myArr(1) = "quick"
      myArr(2) = "brown"
      myArr(3) = "fox"
      myArr(4) = "jumps"
      myArr(5) = "over"
      myArr(6) = "the"
      myArr(7) = "lazy"
      myArr(8) = "dog"

      ' Displays the values of the Array.
      Dim i As Integer = 0
      Dim myEnumerator As System.Collections.IEnumerator = myArr.GetEnumerator()
      Console.WriteLine("The Array contains the following values:")
      While myEnumerator.MoveNext() And Not (myEnumerator.Current Is Nothing)
         Console.WriteLine("[{0}] {1}", i, myEnumerator.Current)
         i += 1
      End While 

   End Sub

End Class


'This code produces the following output.
'
'The Array contains the following values:
'[0] The
'[1] quick
'[2] brown
'[3] fox
'[4] jumps
'[5] over
'[6] the
'[7] lazy
'[8] dog

注解

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

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

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

在调用 CurrentMoveNext 之前,Reset 返回同一对象。 MoveNextCurrent 设置为下一个元素。

如果 MoveNext 传递集合的末尾,则枚举器将定位在集合中的最后一个元素之后并 MoveNext 返回 false。 当枚举器处于此位置时,后续调用 MoveNext 也会返回 false。 如果返回false的最后一次调用MoveNextCurrent则为未定义。 若要再次将 Current 设置为集合的第一个元素,可以调用 Reset 并接着调用 MoveNext

只要集合保持不变,枚举器就仍有效。 如果对集合进行更改(如添加、修改或删除元素),则枚举数将失效且不可恢复,而且其行为是不确定的。

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

此方法是一个 O (1) 操作。

适用于