📜  C#|获取一个遍历HybridDictionary的枚举数

📅  最后修改于: 2021-05-29 21:58:49             🧑  作者: Mango

HybridDictionary.GetEnumerator方法用于返回迭代HybridDictionary的IDictionaryEnumerator。

句法:

public System.Collections.IDictionaryEnumerator GetEnumerator ();

返回值:它为HybridDictionary返回一个IDictionaryEnumerator(枚举非通用字典元素的接口)。

下面的程序说明了HybridDictionary.GetEnumerator方法的用法:

范例1:

// C# code to get an IDictionaryEnumerator
// that iterates through the HybridDictionary.
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a HybridDictionary named myDict
        HybridDictionary myDict = new HybridDictionary();
  
        // Adding key/value pairs in myDict
        myDict.Add("A", "Apple");
        myDict.Add("B", "Banana");
        myDict.Add("C", "Cat");
        myDict.Add("D", "Dog");
        myDict.Add("E", "Elephant");
        myDict.Add("F", "Fish");
  
        // To get an IDictionaryEnumerator
        // for the HybridDictionary.
        IDictionaryEnumerator myEnumerator = myDict.GetEnumerator();
  
        // If MoveNext passes the end of the
        // collection, the enumerator is positioned
        // after the last element in the collection
        // and MoveNext returns false.
        while (myEnumerator.MoveNext())
            Console.WriteLine(myEnumerator.Key + " --> " 
                                  + myEnumerator.Value);
    }
}

输出:

A --> Apple
B --> Banana
C --> Cat
D --> Dog
E --> Elephant
F --> Fish

范例2:

// C# code to get an IDictionaryEnumerator
// that iterates through the HybridDictionary.
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a HybridDictionary named myDict
        HybridDictionary myDict = new HybridDictionary();
  
        // Adding key/value pairs in myDict
        myDict.Add("I", "first");
        myDict.Add("II", "second");
        myDict.Add("III", "third");
        myDict.Add("IV", "fourth");
        myDict.Add("V", "fifth");
  
        // To get an IDictionaryEnumerator
        // for the HybridDictionary.
        IDictionaryEnumerator myEnumerator = myDict.GetEnumerator();
  
        // If MoveNext passes the end of the
        // collection, the enumerator is positioned
        // after the last element in the collection
        // and MoveNext returns false.
        while (myEnumerator.MoveNext())
            Console.WriteLine(myEnumerator.Key + " --> " 
                                  + myEnumerator.Value);
    }
}

输出:

I --> first
II --> second
III --> third
IV --> fourth
V --> fifth

笔记:

  • C#语言的foreach语句隐藏了枚举器的复杂性。因此,建议使用foreach,而不是直接操作枚举器。
  • 枚举数可用于读取集合中的数据,但不能用于修改基础集合。
  • 在调用MoveNextReset之前,Current返回相同的对象。 MoveNext将Current设置为下一个元素。
  • 只要集合保持不变,枚举数将保持有效。如果对集合进行了更改(例如添加,修改或删除元素),则枚举数将无法恢复,并且其行为是不确定的。
  • 此方法是O(1)操作。

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.specialized.hybriddictionary.getenumerator?view=netframework-4.7.2