Array.GetEnumerator 方法

定義

回傳 和 IEnumerator 表示 Array

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

傳回

A IEnumerator 代表 Array

實作

範例

以下程式碼範例展示了如何使用 GetEnumerator 來列出陣列元素。

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

備註

C# 語言的 foreach 陳述(Visual Basic 中為 For Each)隱藏了列舉器的複雜性。 因此,建議使用 foreach ,而非直接操作列舉器。

列舉器可用於讀取集合中的資料,但無法用來修改底層集合。

最初,列舉器會被放置在集合中的第一個元素之前。 Reset 同時也讓普查員回到這個位置。 在此位置, Current 未定義。 因此,您必須先呼叫 MoveNext 列舉器先將列舉器推進到集合的第一個元素,再讀取 的 Current值。

Current 回傳相同物件,直到被呼叫其中一 MoveNextResetMoveNext 設定 Current 為下一個元素。

MoveNext 通過集合的末尾,列舉器會位於集合的最後一個元素之後,並 MoveNext 返回 false。 當列舉器處於此位置時,後續呼叫 MoveNext 也返回 false。 若最後一次呼叫 MoveNext 回傳 falseCurrent 則為未定義。 若要再次設定 Current 為集合的第一個元素,你可以呼叫 Reset ,接著 MoveNext

只要集合未曾改變,列舉器仍然有效。 若對集合進行變更,例如新增、修改或刪除元素,列舉器將無法恢復,其行為也無法定義。

列舉員並非擁有收藏的獨家存取權;因此,透過集合列舉本質上並非執行緒安全的程序。 若要保證列舉期間的線程安全性,您可以在整個列舉期間鎖定集合。 若要允許多個線程存取集合以進行讀取和寫入,您必須實作自己的同步處理。

此方法是 O(1) 作業。

適用於