StringEnumerator.Current 속성

정의

컬렉션의 현재 요소를 가져옵니다.

public:
 property System::String ^ Current { System::String ^ 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 열거자를 호출하여 컬렉션의 첫 번째 요소로 이동한 후 값을 Current읽어야 합니다. 그렇지 않으면 Current 정의되지 MoveNext 않습니다.

Current는 컬렉션의 끝을 나타내는 반환falseMoveNext 마지막 호출인 경우 예외를 throw합니다.

Current에서는 열거자의 위치를 이동하지 않으며, 같은 개체를 호출하거나 Current 호출할 때까지 MoveNext 동일한 개체를 반환하는 Reset 연속 호출이 수행됩니다.

컬렉션이 변경되지 않은 상태로 유지되는 한 열거자는 유효한 상태로 유지됩니다. 컬렉션에 요소를 추가, 수정 또는 삭제하여 변경이 발생하면, 열거자는 복구할 수 없게 무효화되며 다음에 MoveNext 또는 Reset을 호출할 때 InvalidOperationException 예외가 발생합니다. 컬렉션이 수정된 경우 MoveNextCurrentCurrent 열거자가 이미 무효화된 경우에도 컬렉션이 설정된 요소를 반환합니다.

적용 대상

추가 정보