Array.GetEnumerator Metoda

Definicja

Zwraca element IEnumerator dla elementu 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

Zwraca

IEnumerator

Element IEnumerator dla elementu Array.

Implementuje

Przykłady

W poniższym przykładzie kodu pokazano, jak używać GetEnumerator do wyświetlania listy elementów tablicy.

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

Uwagi

Instrukcja foreach języka C# (for each w języku C++For Each, w Visual Basic) ukrywa złożoność modułów wyliczania. W związku z tym użycie foreach jest zalecane, zamiast bezpośrednio manipulować modułem wyliczającym.

Moduły wyliczające mogą służyć do odczytu danych w kolekcji, ale nie można za ich pomocą modyfikować kolekcji źródłowej.

Początkowo moduł wyliczający jest umieszczony przed pierwszym elementem w kolekcji. Reset powoduje również powrót modułu wyliczającego do tej pozycji. Na tym stanowisku Current jest niezdefiniowany. W związku z tym należy wywołać metodę MoveNext , aby przejść do pierwszego elementu kolekcji przed odczytaniem wartości Current.

Current zwraca ten sam obiekt do momentu wywołania metody MoveNext lub Reset . MoveNext ustawia Current wartość na następny element.

Jeśli MoveNext przejdzie koniec kolekcji, moduł wyliczający jest umieszczony po ostatnim elemecie w kolekcji i MoveNext zwraca wartość false. Gdy moduł wyliczający znajduje się na tym stanowisku, kolejne wywołania zwracają MoveNext również polecenie false. Jeśli ostatnie wywołanie, które MoveNext ma być zwrócone false, Current jest niezdefiniowane. Aby ponownie ustawić Current element na pierwszy element kolekcji, możesz wywołać Reset metodę .MoveNext

Moduł wyliczający zachowuje ważność tak długo, jak długo kolekcja pozostaje niezmieniona. Jeśli w kolekcji zostaną wprowadzone zmiany, takie jak dodanie, zmodyfikowanie czy usunięcie elementów, moduł wyliczający jest nieodwracalnie unieważniany, a jego zachowanie staje się niezdefiniowane.

Moduł wyliczający nie ma wyłącznego dostępu do kolekcji; dlatego wyliczanie za pośrednictwem kolekcji nie jest wewnętrznie bezpieczną procedurą wątku. Aby zagwarantować bezpieczeństwo wątków podczas wyliczania, można zablokować kolekcję podczas całego procesu wyliczania. Aby zezwolić wielu wątkom na dostęp do kolekcji w celu odczytu i zapisu danych, należy zaimplementować własny mechanizm synchronizacji.

Ta metoda jest operacją O(1).

Dotyczy