ResourceSet.GetEnumerator 方法

定義

傳回可以逐一查看 ResourceSetIDictionaryEnumerator

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

傳回

這個 IDictionaryEnumeratorResourceSet

屬性

例外狀況

資源集已關閉或已處置。

範例

下列範例示範如何為 檔案 items.resources 建立 ResourceSetrs 。 接下來, GetEnumerator 方法會用來建立 IDictionaryEnumeratorrs 。 會 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 會傳回相同的物件直到呼叫 MoveNextResetMoveNext 會將 Current 設定為下一個項目。

在超過集合的末端後,列舉值會放置在集合最後一個元素之後,並且呼叫 MoveNext 會傳回 false。 如果最後一 MoveNext 次呼叫傳 false 回 ,則呼叫 Current 會擲回例外狀況。 若要再次將 Current 設定為集合的第一個元素,您可以在呼叫 Reset 之後,接著呼叫 MoveNext

只要集合維持不變,列舉值就仍維持有效。 如果對集合進行了變更,例如加入、修改或刪除元素,則列舉值將無法回復地失效,且下次呼叫 MoveNextReset 時,會擲回 InvalidOperationException。 如果在 MoveNextCurrent 之間修改集合,Current 將傳回所設定的元素,即使列舉值已經無效。

您可以使用 IDictionaryEnumerator.Entry 屬性來存取儲存在目前元素中的值。 IDictionaryEnumerator.Key使用 屬性來存取目前專案的索引鍵。 IDictionaryEnumerator.Value使用 屬性來存取目前專案的值。

列舉程式沒有集合的獨佔存取權,因此,列舉集合內容本質上並不是安全的執行緒程序。 即使同步處理集合,其他執行緒仍可修改集合,這會導致列舉值擲回例外狀況。 若要保證列舉過程的執行緒安全,您可以在整個列舉過程中鎖定集合,或攔截由其他執行緒的變更所造成的例外狀況。

適用於