ResourceSet.GetEnumerator メソッド

定義

IDictionaryEnumerator を反復処理できる ResourceSet を返します。

public:
 virtual System::Collections::IDictionaryEnumerator ^ GetEnumerator();
public virtual System.Collections.IDictionaryEnumerator GetEnumerator ();
[System.Runtime.InteropServices.ComVisible(false)]
public virtual System.Collections.IDictionaryEnumerator GetEnumerator ();
abstract member GetEnumerator : unit -> System.Collections.IDictionaryEnumerator
override this.GetEnumerator : unit -> System.Collections.IDictionaryEnumerator
[<System.Runtime.InteropServices.ComVisible(false)>]
abstract member GetEnumerator : unit -> System.Collections.IDictionaryEnumerator
override this.GetEnumerator : unit -> System.Collections.IDictionaryEnumerator
Public Overridable Function GetEnumerator () As IDictionaryEnumerator

戻り値

この ResourceSetIDictionaryEnumerator

属性

例外

リソース セットは、既に閉じられているか破棄されています。

次の例では、 ファイルitems.resourcesの をResourceSetrs作成する方法を示します。 次に、 メソッドを GetEnumerator 使用して の を IDictionaryEnumerator 作成します rs。 は IDictionaryEnumerator 反復処理を行 rs い、内容をコンソールに表示します。

using namespace System;
using namespace System::Resources;
using namespace System::Collections;
int main()
{
   
   // Create a ResourceSet for the file items.resources.
   ResourceSet^ rs = gcnew ResourceSet( "items.resources" );
   
   // Create an IDictionaryEnumerator* to read the data in the ResourceSet.
   IDictionaryEnumerator^ id = rs->GetEnumerator();
   
   // Iterate through the ResourceSet and display the contents to the console.
   while ( id->MoveNext() )
      Console::WriteLine( "\n [{0}] \t {1}", id->Key, id->Value );

   rs->Close();
}
using System;
using System.Resources;
using System.Collections;

class EnumerateResources 
{
    public static void Main() 
    {
        // Create a ResourceSet for the file items.resources.
        ResourceSet rs = new ResourceSet("items.resources"); 

        // Create an IDictionaryEnumerator to read the data in the ResourceSet.
        IDictionaryEnumerator id = rs.GetEnumerator(); 

        // Iterate through the ResourceSet and display the contents to the console. 
        while(id.MoveNext())
          Console.WriteLine("\n[{0}] \t{1}", id.Key, id.Value); 

        rs.Close();
    }
}
Imports System.Resources
Imports System.Collections

Class EnumerateResources
   
   Public Shared Sub Main()
      ' Create a ResourceSet for the file items.resources.
      Dim rs As New ResourceSet("items.resources")      
      
      ' Create an IDictionaryEnumerator to read the data in the ResourceSet.
      Dim id As IDictionaryEnumerator = rs.GetEnumerator()
      
      ' Iterate through the ResourceSet and display the contents to the console. 
      While id.MoveNext()
         Console.WriteLine(ControlChars.NewLine + "[{0}] " + ControlChars.Tab + "{1}", id.Key, id.Value)
      End While 

      rs.Close()

   End Sub

End Class

注釈

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

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

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

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

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

プロパティを使用して、 IDictionaryEnumerator.Entry 現在の要素に格納されている値にアクセスできます。 プロパティを IDictionaryEnumerator.Key 使用して、現在の要素のキーにアクセスします。 プロパティを IDictionaryEnumerator.Value 使用して、現在の要素の値にアクセスします。

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

適用対象