StringEnumerator クラス

定義

StringCollectionに対する単純な繰り返し処理をサポートします。

public ref class StringEnumerator
public class StringEnumerator
type StringEnumerator = class
Public Class StringEnumerator
継承
StringEnumerator

次のコード例では、 のプロパティとメソッドをいくつか示します 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.

注釈

C# 言語の foreach ステートメント (Visual Basic の場合は for each) を使用することで列挙子の複雑さを回避できます。 したがって、列挙子を直接操作するのではなく、foreach を使用することをお勧めします。

列挙子を使用すると、コレクション内のデータを読み取ることができますが、基になるコレクションを変更することはできません。

最初、列挙子はコレクションの先頭の要素の前に位置付けられます。 また、Reset メソッドは、列挙子を最初の位置に戻します。 この位置で Current を呼び出すと、例外がスローされます。 そのため、MoveNext の値を読み取る前に、Current を呼び出して列挙子をコレクションの最初の要素に進める必要があります。

Current は、MoveNext または Reset が呼び出されるまでは同じオブジェクトを返します。 MoveNext は、Current を次の要素に進めます。

MoveNext がコレクションの末尾を通過した場合、列挙子がコレクション内の最後の要素の後に配置され、MoveNextfalse を返します。 列挙子がこの位置にある場合、後続の MoveNext 呼び出しも false を返します。 前回 MoveNext を呼び出して false が返された場合に、Current を呼び出すと例外がスローされます。 コレクションの先頭の要素に再び Current を設定するには、先に Reset を呼び出してから MoveNext を呼び出します。

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

列挙子はコレクションに排他アクセスできないため、コレクションの列挙処理は本質的にスレッド セーフな処理ではありません。 コレクションが同期されていても、他のスレッドがコレクションを変更する場合があり、このときは列挙子から例外がスローされます。 列挙処理を確実にスレッド セーフに行うには、列挙中にコレクションをロックするか、他のスレッドによって行われた変更によってスローされる例外をキャッチします。

プロパティ

Current

コレクション内の現在の要素を取得します。

メソッド

Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
MoveNext()

列挙子をコレクションの次の要素に進めます。

Reset()

列挙子を初期位置、つまりコレクションの最初の要素の前に設定します。

ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

適用対象

スレッド セーフ

パブリック静的 (Visual Basic ではShared) なこの型のメンバーはスレッド セーフです インスタンス メンバーの場合は、スレッド セーフであるとは限りません。

コレクションの列挙処理は、本質的にスレッドセーフな処理ではありません。 コレクションが同期されていても、他のスレッドがコレクションを変更する場合があり、このときは列挙子から例外がスローされます。 列挙処理を確実にスレッド セーフに行うには、列挙中にコレクションをロックするか、他のスレッドによって行われた変更によってスローされる例外をキャッチします。

こちらもご覧ください