OrderedDictionary Class in C#

CsharpServer Side ProgrammingProgramming

The OrderedDictionary class represents a collection of key/value pairs that are accessible by the key or index.

Following are the properties of OrderedDictionary class −

Sr.noProperty & Description
1Count
Gets the number of key/values pairs contained in the OrderedDictionary collection.
2IsReadOnly
Gets a value indicating whether the OrderedDictionary collection is read-only.
3Item[Int32]
Gets or sets the value at the specified index.
4Item[Object]
Gets or sets the value with the specified key.
5Keys
Gets an ICollection object containing the keys in the OrderedDictionary collection.
6Values
Gets an ICollection object containing the values in the OrderedDictionary collection.

Following are some of the methods of the OrderedDictionary class −

Sr.noMethod & Description
1Add(Object, Object)
Adds an entry with the specified key and value into the OrderedDictionary collection with the lowest available index.
2AsReadOnly()
Returns a read-only copy of the current OrderedDictionary collection.
3Clear()
Removes all elements from the OrderedDictionary collection.
4Contains(Object)
Determines whether the OrderedDictionary collection contains a specific key.
5CopyTo(Array, Int32)
Copies the OrderedDictionary elements to a onedimensional Array object at the specified index.
6Equals(Object)
Determines whether the specified object is equal to the current object. (Inherited from Object)
7GetEnumerator()
Returns an IDictionaryEnumerator object that iterates through the OrderedDictionary collection.

Let us now see some examples −

Example

To get the number of key/values pairs contained in OrderedDictionary, the code is as follows −

 Live Demo

using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
   public static void Main() {
      OrderedDictionary dict = new OrderedDictionary();
      dict.Add("A", "Home Appliances");
      dict.Add("B", "Electronics");
      dict.Add("C", "Smart Wearables");
      dict.Add("D", "Pet Supplies");
      dict.Add("E", "Clothing");
      dict.Add("F", "Footwear");
      Console.WriteLine("OrderedDictionary elements...");
      foreach(DictionaryEntry d in dict) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("Count of elements in OrderedDictionary = " + dict.Count);
      dict.Clear();
      Console.WriteLine("Count of elements in OrderedDictionary (Updated)= " + dict.Count);
   }
}

Output

This will produce the following output −

OrderedDictionary elements...
A Home Appliances
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear
Count of elements in OrderedDictionary = 6
Count of elements in OrderedDictionary (Updated)= 0

Example

To remove all elements from OrderedDictionary, the code is as follows −

 Live Demo

using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
   public static void Main() {
      OrderedDictionary dict = new OrderedDictionary();
      dict.Add("A", "Books");
      dict.Add("B", "Electronics");
      dict.Add("C", "Smart Wearables");
      dict.Add("D", "Pet Supplies");
      dict.Add("E", "Clothing");
      dict.Add("F", "Footwear");
      Console.WriteLine("OrderedDictionary elements...");
      foreach(DictionaryEntry d in dict) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("Count of elements in OrderedDictionary = " + dict.Count);
      dict.Clear();
      Console.WriteLine("Count of elements in OrderedDictionary (Updated)= " + dict.Count);
   }
}

Output

This will produce the following output −

OrderedDictionary elements...
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear
Count of elements in OrderedDictionary = 6
Count of elements in OrderedDictionary (Updated)= 0
raja
Published on 10-Dec-2019 07:54:44
Advertisements