StringEnumerator.Current Propriedade

Definição

Obtém o elemento atual na coleção.

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

Valor da propriedade

O elemento atual da coleção.

Exceções

O enumerador é posicionado antes do primeiro elemento da coleção ou depois do último elemento.

Exemplos

O exemplo de código a seguir demonstra várias das propriedades e métodos de 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.

Comentários

Depois que um enumerador é criado ou depois que um Reset é chamado, MoveNext deve ser chamado para avançar o enumerador para o primeiro elemento da coleção antes de ler o valor de Current; caso contrário, Current é indefinido.

Current também gera uma exceção se a última chamada para MoveNext retornada false, o que indica o final da coleção.

Current não move a posição do enumerador e chamadas consecutivas para Current retornar o mesmo objeto até MoveNext ou Reset é chamado.

Um enumerador permanece válido desde que a coleção permaneça inalterada. Se forem feitas alterações na coleção, como adicionar, modificar ou excluir elementos, o enumerador será invalidado irrecuperavelmente e a próxima chamada para MoveNext ou Reset lançará um InvalidOperationException. Se a coleção for modificada entre MoveNext e Current, Current retornará o elemento para o qual está definida, mesmo que o enumerador já esteja invalidado.

Aplica-se a

Confira também