StringEnumeratorとは? わかりやすく解説

StringEnumerator クラス

StringCollection に対する単純な反復処理をサポートします。

名前空間: System.Collections.Specialized
アセンブリ: System (system.dll 内)
構文構文

Public Class StringEnumerator
Dim instance As StringEnumerator
public class StringEnumerator
public ref class StringEnumerator
public class StringEnumerator
public class StringEnumerator
解説解説

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

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

初期状態では、列挙子はコレクションの最初の要素の前に位置しています。Reset を実行した場合も、列挙子はこの位置に戻されます。この位置で Current を呼び出すと、例外がスローされます。したがって、Current の値を読み取る前に、MoveNext を呼び出して、コレクションの最初の要素に列挙子を進める必要があります。

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

MoveNext がコレクションの末尾を過ぎると、列挙子はコレクションの最後の要素の後ろに配置され、MoveNext は false を返します。列挙子がこの位置にある場合、以降、MoveNext を呼び出しても false が返されます。MoveNext への最後の呼び出しで false が返された場合に、Current を呼び出すと例外がスローされます。Current をコレクションの最初の要素に再び設定するには、Reset を呼び出してから、MoveNext を呼び出します。

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

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

使用例使用例

StringEnumerator のプロパティとメソッドのいくつかの例を次に示します。

Imports System
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 'Main

End Class 'SamplesStringEnumerator
 


'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.

*/
#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.

*/
import System.* ;
import System.Collections.Specialized.* ;

public class SamplesStringEnumerator
{
    public static void main(String[]
 args)
    {
        // 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.get_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.get_Current());
        }
    } //main 
} //SamplesStringEnumerator

/*
This code produces the following output.

red
orange
yellow
green
blue
indigo
violet

The first element is red.

*/
継承階層継承階層
System.Object
  System.Collections.Specialized.StringEnumerator
スレッド セーフスレッド セーフ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
StringEnumerator メンバ
System.Collections.Specialized 名前空間
IEnumerable インターフェイス
IEnumerator インターフェイス
StringCollection クラス

StringEnumerator プロパティ


パブリック プロパティパブリック プロパティ

参照参照

関連項目

StringEnumerator クラス
System.Collections.Specialized 名前空間
IEnumerable インターフェイス
IEnumerator インターフェイス
StringCollection クラス

StringEnumerator メソッド


パブリック メソッドパブリック メソッド

プロテクト メソッドプロテクト メソッド
参照参照

関連項目

StringEnumerator クラス
System.Collections.Specialized 名前空間
IEnumerable インターフェイス
IEnumerator インターフェイス
StringCollection クラス

StringEnumerator メンバ

StringCollection に対する単純な反復処理をサポートします。

StringEnumerator データ型で公開されるメンバを以下の表に示します。


パブリック プロパティパブリック プロパティ
パブリック メソッドパブリック メソッド
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

StringEnumerator クラス
System.Collections.Specialized 名前空間
IEnumerable インターフェイス
IEnumerator インターフェイス
StringCollection クラス



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「StringEnumerator」の関連用語

StringEnumeratorのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



StringEnumeratorのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS