StringEnumerator.Current Свойство

Определение

Возвращает текущий элемент коллекции.

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

Значение свойства

Текущий элемент коллекции.

Исключения

Перечислитель размещается перед первым элементом коллекции или после последнего элемента.

Примеры

В следующем примере кода демонстрируется несколько свойств и методов 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.

Комментарии

После создания перечислителя или после Reset вызова MoveNext необходимо вызвать перечислитель к первому элементу коллекции, прежде чем считывать значение Current; в противном случае Current не определено.

Current также вызывает исключение, если последний вызов MoveNext возвращаемого falseобъекта, указывающий конец коллекции.

Current не перемещает позицию перечислителя и последовательные вызовы для Current возврата одного и того же объекта до MoveNext вызова или Reset вызова.

Перечислитель остается допустимым, пока коллекция остается неизменной. Если изменения вносятся в коллекцию, например добавление, изменение или удаление элементов, перечислитель неустранимо недопустим, а следующий вызов MoveNext или вызов вызывает ResetисключениеInvalidOperationException. Если коллекция изменяется между MoveNext и CurrentCurrent возвращает элемент, которому он задан, даже если перечислитель уже недопустим.

Применяется к

См. также раздел