C# | OrderedDictionary Class

24 Jan, 2019
OrderedDictionary Class represents a collection of key/value pairs that are accessible by the key or index. It is present in System.Collections.Specialized namespace.
Properties of OrderedDictionary Class :
Each element is a key/value pair stored in a DictionaryEntry object.
A key cannot be null, but a value can be.
The elements of an OrderedDictionary are not sorted by the key.
Elements can be accessed either by the key or by the index.
Example:
// C# code to create a OrderedDictionaryusing System;using System.Collections;using System.Collections.Specialized;  class GFG {      // Driver method    public static void Main()    {          // Creating a orderedDictionary named myDict        OrderedDictionary myDict = new OrderedDictionary();          // Adding key and value in myDict        myDict.Add("1", "ONE");        myDict.Add("2", "TWO");        myDict.Add("3", "THREE");          // Displaying the number of key/value        // pairs in myDict        Console.WriteLine(myDict.Count);          // Displaying the key/value pairs in myDict        foreach(DictionaryEntry de in myDict)            Console.WriteLine(de.Key + " --> " + de.Value);    }}
Output:
3
1 --> ONE
2 --> TWO
3 --> THREE

Example 1:
// C# code to check if OrderedDictionary// collection is read-onlyusing System;using System.Collections;using System.Collections.Specialized;  class GFG {      // Driver method    public static void Main()    {          // Creating a orderedDictionary named myDict        OrderedDictionary myDict = new OrderedDictionary();          // Adding key and value in myDict        myDict.Add("key1", "value1");        myDict.Add("key2", "value2");        myDict.Add("key3", "value3");        myDict.Add("key4", "value4");        myDict.Add("key5", "value5");          // Checking if OrderedDictionary        // collection is read-only        Console.WriteLine(myDict.IsReadOnly);    }}
Output:
False

Example 2:
// C# code to get the number of// key/values pairs contained// in the OrderedDictionaryusing System;using System.Collections;using System.Collections.Specialized;  class GFG {      // Driver method    public static void Main()    {          // Creating a orderedDictionary named myDict        OrderedDictionary myDict = new OrderedDictionary();          // Adding key and value in myDict        myDict.Add("A", "Apple");        myDict.Add("B", "Banana");        myDict.Add("C", "Cat");        myDict.Add("D", "Dog");          // To Get the number of key/values        // pairs contained in the OrderedDictionary        Console.WriteLine("Number of key/value pairs are : "                                             + myDict.Count);    }}
Output:
Number of key/value pairs are : 4

Example 1:
// C# code to get a read-only// copy of the OrderedDictionaryusing System;using System.Collections;using System.Collections.Specialized;  class GFG {      // Driver method    public static void Main()    {          // Creating a orderedDictionary named myDict        OrderedDictionary myDict = new OrderedDictionary();          // Adding key and value in myDict        myDict.Add("key1", "value1");        myDict.Add("key2", "value2");        myDict.Add("key3", "value3");        myDict.Add("key4", "value4");        myDict.Add("key5", "value5");          // To Get a read-only copy of        // the OrderedDictionary        OrderedDictionary myDict_1 = myDict.AsReadOnly();          // Checking if myDict_1 is read-only        Console.WriteLine(myDict_1.IsReadOnly);    }}
Output:
True

Example 2:
// C# code to remove the entry// with the specified key from// the OrderedDictionaryusing System;using System.Collections;using System.Collections.Specialized;  class GFG {      // Driver method    public static void Main()    {          // Creating a orderedDictionary named myDict        OrderedDictionary myDict = new OrderedDictionary();          // Adding key and value in myDict        myDict.Add("key1", "value1");        myDict.Add("key2", "value2");        myDict.Add("key3", "value3");        myDict.Add("key4", "value4");        myDict.Add("key5", "value5");          // Displaying the number of element initially        Console.WriteLine("Number of elements are : " + myDict.Count);          // Displaying the elements in myDict        foreach(DictionaryEntry de in myDict)            Console.WriteLine(de.Key + " -- " + de.Value);          // Removing the entry with the specified        // key from the OrderedDictionary        myDict.Remove("key2");          // Displaying the number of element initially        Console.WriteLine("Number of elements are : " + myDict.Count);          // Displaying the elements in myDict        foreach(DictionaryEntry de in myDict)            Console.WriteLine(de.Key + " -- " + de.Value);    }}
Output:
Number of elements are : 5
key1 -- value1
key2 -- value2
key3 -- value3
key4 -- value4
key5 -- value5
Number of elements are : 4
key1 -- value1
key3 -- value3
key4 -- value4
key5 -- value5

Reference:
https://docs.microsoft.com/en-us/dotnet/api/system.collections.specialized.ordereddictionary?view=netframework-4.7.2
CSharp-Specialized-Namespace
CSharp-Specialized-OrderedDictionary
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.