OrderedDictionary Class in C#



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

Following are some of the methods of the OrderedDictionary class −

Sr.no Method & Description
1 Add(Object, Object)
Adds an entry with the specified key and value into the OrderedDictionary collection with the lowest available index.
2 AsReadOnly()
Returns a read-only copy of the current OrderedDictionary collection.
3 Clear()
Removes all elements from the OrderedDictionary collection.
4 Contains(Object)
Determines whether the OrderedDictionary collection contains a specific key.
5 CopyTo(Array, Int32)
Copies the OrderedDictionary elements to a onedimensional Array object at the specified index.
6 Equals(Object)
Determines whether the specified object is equal to the current object. (Inherited from Object)
7 GetEnumerator()
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
Updated on: 2019-12-10T07:56:50+05:30

884 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements