Share via


StringEnumerator.Current Vlastnost

Definice

Získá aktuální prvek v kolekci.

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

Hodnota vlastnosti

Aktuální prvek v kolekci.

Výjimky

Enumerátor je umístěn před prvním prvkem kolekce nebo za poslední prvek.

Příklady

Následující příklad kódu ukazuje několik vlastností a metod nástroje 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.

Poznámky

Po vytvoření enumerátoru nebo po Reset zavolání musí být volána, MoveNext aby se enumerátor na první prvek kolekce před čtením hodnoty Current. Jinak Current není definován.

Current vyvolá také výjimku, pokud poslední volání MoveNext vrátí false, což označuje konec kolekce.

Current neposouvá pozici enumerátoru a po sobě jdoucí volání vrátit Current stejný objekt, dokud není volána nebo MoveNextReset .

Enumerátor zůstane platný, dokud kolekce zůstane beze změny. Pokud jsou v kolekci provedeny změny, například přidání, úprava nebo odstranění prvků, je výčet nenávratně zneplatněný a další volání MoveNext nebo Reset vyvolá InvalidOperationException. Pokud je kolekce změněna mezi MoveNext a Current, Current vrátí prvek, na který je nastavena, i když je výčet již neplatný.

Platí pro

Viz také