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

📅  最后修改于: 2021-05-30 00:18:10             🧑  作者: Mango

StringDictionary.GetEnumerator方法用于返回迭代字符串字典的枚举数。

句法:

public virtual System.Collections.IEnumerator GetEnumerator ();

返回值:迭代字符串字典的IEnumerator。

下面给出了一些示例,以更好地理解实现:

范例1:

// C# code to get an enumerator
// that iterates through the stringDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a StringDictionary named myDict
        StringDictionary myDict = new StringDictionary();
  
        // Adding key and value into the StringDictionary
        myDict.Add("A", "Apple");
        myDict.Add("B", "Banana");
        myDict.Add("C", "Cat");
        myDict.Add("D", "Dog");
  
        // "IEnumerator" interface supports a simple
        // iteration over a non-generic collection.
        IEnumerator myEnumerator = myDict.GetEnumerator();
  
        DictionaryEntry de;
  
        // "MoveNext" advances the enumerator
        // to the next element of the collection.
        // you must call "MoveNext" to advance the
        // enumerator to the first element of the
        // collection before reading the value of "Current"
        while (myEnumerator.MoveNext()) {
  
            // "Current" returns the same object until
            // either "MoveNext" is called.
            // "MoveNext" sets "Current" to the next element.
            de = (DictionaryEntry)myEnumerator.Current;
            Console.WriteLine(de.Key + " " + de.Value);
        }
    }
}

输出:

d Dog
b Banana
c Cat
a Apple

范例2:

// C# code to get an enumerator
// that iterates through the stringDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a StringDictionary named myDict
        StringDictionary myDict = new StringDictionary();
  
        // Adding key and value into the StringDictionary
        myDict.Add("I", "one");
        myDict.Add("II", "two");
        myDict.Add("III", "three");
        myDict.Add("IV", "four");
        myDict.Add("V", "five");
  
        // "IEnumerator" interface supports a simple
        // iteration over a non-generic collection.
        IEnumerator myEnumerator = myDict.GetEnumerator();
  
        DictionaryEntry de;
  
        // "MoveNext" advances the enumerator
        // to the next element of the collection.
        // you must call "MoveNext" to advance the
        // enumerator to the first element of the
        // collection before reading the value of "Current"
        while (myEnumerator.MoveNext()) {
  
            // "Current" returns the same object until
            // either "MoveNext" is called.
            // "MoveNext" sets "Current" to the next element.
            de = (DictionaryEntry)myEnumerator.Current;
            Console.WriteLine(de.Key + " " + de.Value);
        }
    }
}

输出:

iv four
i one
iii three
v five
ii two

笔记:

  • 枚举数可用于读取集合中的数据,但不能用于修改基础集合。
  • 只要集合保持不变,枚举数将保持有效。如果对集合进行了更改(例如添加,修改或删除元素),则枚举数将无法恢复,并且其行为是不确定的。
  • 此方法是O(1)操作。

参考:

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