Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
OrderedDictionary Class in C#
The OrderedDictionary class in C# represents a collection of key/value pairs that maintains insertion order and allows access by both key and index. It combines the functionality of a dictionary with the ordered nature of a list, making it useful when you need to preserve the order of items while still providing fast key-based lookup.
Syntax
Following is the syntax for creating and using an OrderedDictionary −
OrderedDictionary dict = new OrderedDictionary(); dict.Add(key, value); dict[key] = value; // Access by key dict[index] = value; // Access by index
Properties
Following are the key properties of the OrderedDictionary class −
| Property | Description |
|---|---|
| Count | Gets the number of key/value pairs contained in the OrderedDictionary collection. |
| IsReadOnly | Gets a value indicating whether the OrderedDictionary collection is read-only. |
| Item[Int32] | Gets or sets the value at the specified index. |
| Item[Object] | Gets or sets the value with the specified key. |
| Keys | Gets an ICollection object containing the keys in the OrderedDictionary collection. |
| Values | Gets an ICollection object containing the values in the OrderedDictionary collection. |
Key Methods
| Method | Description |
|---|---|
| Add(Object, Object) | Adds an entry with the specified key and value into the OrderedDictionary collection. |
| Clear() | Removes all elements from the OrderedDictionary collection. |
| Contains(Object) | Determines whether the OrderedDictionary collection contains a specific key. |
| Remove(Object) | Removes the entry with the specified key from the OrderedDictionary collection. |
| GetEnumerator() | Returns an IDictionaryEnumerator object that iterates through the OrderedDictionary collection. |
Using OrderedDictionary with Count Property
Example
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);
}
}
The output of the above code is −
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
Using Index-Based Access
Example
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
OrderedDictionary dict = new OrderedDictionary();
dict.Add("First", "Apple");
dict.Add("Second", "Banana");
dict.Add("Third", "Cherry");
Console.WriteLine("Access by key:");
Console.WriteLine("First: " + dict["First"]);
Console.WriteLine("\nAccess by index:");
Console.WriteLine("Index 0: " + dict[0]);
Console.WriteLine("Index 1: " + dict[1]);
Console.WriteLine("Index 2: " + dict[2]);
Console.WriteLine("\nModifying value at index 1:");
dict[1] = "Blueberry";
Console.WriteLine("Index 1 now: " + dict[1]);
Console.WriteLine("Key 'Second' now: " + dict["Second"]);
}
}
The output of the above code is −
Access by key: First: Apple Access by index: Index 0: Apple Index 1: Banana Index 2: Cherry Modifying value at index 1: Index 1 now: Blueberry Key 'Second' now: Blueberry
Using Contains and Remove Methods
Example
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
OrderedDictionary dict = new OrderedDictionary();
dict.Add("X", "Mathematics");
dict.Add("Y", "Physics");
dict.Add("Z", "Chemistry");
Console.WriteLine("Initial dictionary:");
foreach(DictionaryEntry entry in dict) {
Console.WriteLine(entry.Key + ": " + entry.Value);
}
Console.WriteLine("\nChecking if key 'Y' exists: " + dict.Contains("Y"));
Console.WriteLine("Checking if key 'W' exists: " + dict.Contains("W"));
dict.Remove("Y");
Console.WriteLine("\nAfter removing key 'Y':");
foreach(DictionaryEntry entry in dict) {
Console.WriteLine(entry.Key + ": " + entry.Value);
}
}
}
The output of the above code is −
Initial dictionary: X: Mathematics Y: Physics Z: Chemistry Checking if key 'Y' exists: True Checking if key 'W' exists: False After removing key 'Y': X: Mathematics Z: Chemistry
Conclusion
The OrderedDictionary class in C# provides the combined benefits of a dictionary's key-based access and a list's ordered structure. It maintains insertion order while allowing access by both key and index, making it ideal for scenarios where order matters alongside fast lookups.
