Use IDictionaryEnumerator to loop through OrderedDictionary : OrderedDictionary « Collections Data Structure « C# / C Sharp






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

1.OrderedDictionary Class Represents a collection of key/value pairs that are accessible by the key or index.
2.Remove element from OrderedDictionary