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

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



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「StringEnumerator クラス」の関連用語

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

   

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



StringEnumerator クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2025 GRAS Group, Inc.RSS