Remove element from OrderedDictionary : OrderedDictionary « Collections Data Structure « C# / C Sharp






Remove element from 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");

        ICollection keyCollection = myOrderedDictionary.Keys;
        ICollection valueCollection = myOrderedDictionary.Values;

     if (!myOrderedDictionary.IsReadOnly)
        {
            myOrderedDictionary.Insert(0, "insertedKey1", "insertedValue1");
            myOrderedDictionary["testKey2"] = "modifiedValue";
            myOrderedDictionary.RemoveAt(myOrderedDictionary.Count - 1);
            if (myOrderedDictionary.Contains("keyToDelete"))
            {
                myOrderedDictionary.Remove("keyToDelete");
            }
        }


    }
    public static void DisplayContents(ICollection keyCollection, ICollection valueCollection, int dictionarySize)
    {
        String[] myKeys = new String[dictionarySize];
        String[] myValues = new String[dictionarySize];
        keyCollection.CopyTo(myKeys, 0);
        valueCollection.CopyTo(myValues, 0);

        for (int i = 0; i < dictionarySize; i++)
        {
            Console.WriteLine("   {0,-5} {1,-25} {2}",i, myKeys[i], myValues[i]);
        }
    }
}

   
  








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.Use IDictionaryEnumerator to loop through OrderedDictionary