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の値を読み取る前に列挙子をコレクションの最初の要素に進めるためにMoveNextを呼び出す必要があります。それ以外の場合、Currentは未定義です。

Current また、 MoveNext の最後の呼び出しが false返された場合も例外がスローされます。これはコレクションの末尾を示します。

Currentは列挙子の位置を移動せず、CurrentまたはMoveNextが呼び出されるまで、Resetの連続した呼び出しは同じオブジェクトを返します。

列挙子は、コレクションが変更されない限り有効なままです。 要素の追加、変更、削除など、コレクションに変更が加えられた場合、列挙子は回復不能に無効になり、次に MoveNext または Reset 呼び出すと InvalidOperationExceptionがスローされます。 コレクションが MoveNextCurrentの間で変更された場合、列挙子が既に無効になっている場合でも、 Current は設定されている要素を返します。

適用対象

こちらもご覧ください