StringEnumerator.Current Vlastnost
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
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ím prvkem.
Příklady
Následující příklad kódu ukazuje několik vlastností a metod StringEnumerator.
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 byl výčet před prvním prvkem kolekce před čtením hodnoty Current; jinak Current není definován.
Current vyvolá také výjimku, pokud poslední volání vráceno MoveNextfalse, což označuje konec kolekce.
Current nepřesune pozici enumerátoru a po sobě jdoucí volání, která Current vrátí stejný objekt, dokud není MoveNext volána nebo Reset volána.
Enumerátor zůstane platný, dokud kolekce zůstane beze změny. Pokud jsou v kolekci provedeny změny, například přidání, úpravy nebo odstranění prvků, enumerátor je nenávratně neplatný a další volání MoveNext nebo Reset vyvolá výjimku InvalidOperationException. Pokud je kolekce změněna mezi MoveNext a Current, Current vrátí prvek, na který je nastaven, i když je enumerátor již neplatný.