共用方式為


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

IEnumeratorArray

實作

範例

下列程式碼範例示範如何使用 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

備註

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

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

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

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

如果 MoveNext 傳遞集合結尾,列舉值會放在集合的最後一個專案後面,並 MoveNextfalse 回 。 當列舉值位於這個位置時,後續呼叫 MoveNext 也會傳回 false 。 如果最後一 MoveNext 次呼叫傳 false 回 , Current 則為未定義。 若要再次將 Current 設定為集合的第一個元素,您可以在呼叫 Reset 之後,接著呼叫 MoveNext

只要集合維持不變,列舉值就仍維持有效。 如果對集合進行變更,例如加入、修改或刪除項目,列舉程式會永久失效,且其行為未定義。

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

這個方法是 O (1) 作業。

適用於