StringEnumerator.Current Właściwość

Definicja

Pobiera bieżący element w kolekcji.

public:
 property System::String ^ Current { System::String ^ get(); };
public string Current { get; }
public string? Current { get; }
member this.Current : string
Public ReadOnly Property Current As String

Wartość właściwości

Bieżący element w kolekcji.

Wyjątki

Moduł wyliczający jest umieszczony przed pierwszym elementem kolekcji lub po ostatnim elemecie.

Przykłady

Poniższy przykład kodu przedstawia kilka właściwości i metod .StringEnumerator

#using <System.dll>

using namespace System;
using namespace System::Collections::Specialized;
int main()
{
   
   // Creates and initializes a StringCollection.
   StringCollection^ myCol = gcnew StringCollection;
   array<String^>^myArr = {"red","orange","yellow","green","blue","indigo","violet"};
   myCol->AddRange( myArr );
   
   // Enumerates the elements in the StringCollection.
   StringEnumerator^ myEnumerator = myCol->GetEnumerator();
   while ( myEnumerator->MoveNext() )
      Console::WriteLine( "{0}", myEnumerator->Current );

   Console::WriteLine();
   
   // Resets the enumerator and displays the first element again.
   myEnumerator->Reset();
   if ( myEnumerator->MoveNext() )
      Console::WriteLine( "The first element is {0}.", myEnumerator->Current );
}

/*
This code produces the following output.

red
orange
yellow
green
blue
indigo
violet

The first element is red.

*/
using System;
using System.Collections.Specialized;

public class SamplesStringEnumerator  {

   public static void Main()  {

      // Creates and initializes a StringCollection.
      StringCollection myCol = new StringCollection();
      String[] myArr = new String[] { "red", "orange", "yellow", "green", "blue", "indigo", "violet" };
      myCol.AddRange( myArr );

      // Enumerates the elements in the StringCollection.
      StringEnumerator myEnumerator = myCol.GetEnumerator();
      while ( myEnumerator.MoveNext() )
         Console.WriteLine( "{0}", myEnumerator.Current );
      Console.WriteLine();

      // Resets the enumerator and displays the first element again.
      myEnumerator.Reset();
      if ( myEnumerator.MoveNext() )
         Console.WriteLine( "The first element is {0}.", myEnumerator.Current );
   }
}

/*
This code produces the following output.

red
orange
yellow
green
blue
indigo
violet

The first element is red.

*/
Imports System.Collections.Specialized

Public Class SamplesStringEnumerator

   Public Shared Sub Main()

      ' Creates and initializes a StringCollection.
      Dim myCol As New StringCollection()
      Dim myArr() As [String] = {"red", "orange", "yellow", "green", "blue", "indigo", "violet"}
      myCol.AddRange(myArr)

      ' Enumerates the elements in the StringCollection.
      Dim myEnumerator As StringEnumerator = myCol.GetEnumerator()
      While myEnumerator.MoveNext()
         Console.WriteLine("{0}", myEnumerator.Current)
      End While
      Console.WriteLine()

      ' Resets the enumerator and displays the first element again.
      myEnumerator.Reset()
      If myEnumerator.MoveNext() Then
         Console.WriteLine("The first element is {0}.", myEnumerator.Current)
      End If 

   End Sub

End Class


'This code produces the following output.
'
'red
'orange
'yellow
'green
'blue
'indigo
'violet
'
'The first element is red.

Uwagi

Po utworzeniu modułu wyliczającego lub po Reset wywołaniu elementu należy wywołać metodę , MoveNext aby przejść moduł wyliczający do pierwszego elementu kolekcji przed odczytaniem wartości Current; w przeciwnym razie Current jest niezdefiniowany.

Current Zgłasza również wyjątek, jeśli ostatnie wywołanie MoveNext zwracane false, co oznacza koniec kolekcji.

Currentnie przenosi położenia modułu wyliczającego, a kolejne wywołania w celu zwrócenia tego samego obiektu do momentu wywołania Current metody lub Reset .MoveNext

Moduł wyliczający zachowuje ważność tak długo, jak długo kolekcja pozostaje niezmieniona. W przypadku wprowadzania zmian w kolekcji, takich jak dodawanie, modyfikowanie lub usuwanie elementów, moduł wyliczający jest nieodwracalnie unieważniany, a następne wywołanie MoveNext metody lub Reset zgłasza InvalidOperationExceptionbłąd . Jeśli kolekcja jest modyfikowana między elementami MoveNext i Current, Current zwraca element ustawiony na , nawet jeśli moduł wyliczający jest już unieważniony.

Dotyczy

Zobacz też