Use IDictionaryEnumerator to loop through OrderedDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
public class OrderedDictionarySample
{
public static void Main()
{
OrderedDictionary myOrderedDictionary = new OrderedDictionary();
myOrderedDictionary.Add("testKey1", "testValue1");
myOrderedDictionary.Add("testKey2", "testValue2");
myOrderedDictionary.Add("keyToDelete", "valueToDelete");
myOrderedDictionary.Add("testKey3", "testValue3");
myOrderedDictionary.Add("newKey1", "newValue1");
myOrderedDictionary.Add("newKey2", "newValue2");
myOrderedDictionary.Add("newKey3", "newValue3");
IDictionaryEnumerator myEnumerator =myOrderedDictionary.GetEnumerator();
DisplayEnumerator(myEnumerator);
}
public static void DisplayEnumerator(IDictionaryEnumerator myEnumerator)
{
while (myEnumerator.MoveNext())
{
Console.WriteLine(" {0,-25} {1}",myEnumerator.Key, myEnumerator.Value);
}
}
}
Related examples in the same category