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
How to create an OrderedDictionary in C#?
An OrderedDictionary in C# is a specialized collection that maintains the insertion order of key-value pairs. Unlike a regular Dictionary, it preserves the sequence in which elements were added, making it useful when order matters.
The OrderedDictionary class is part of the System.Collections.Specialized namespace and provides both indexed access and key-based access to elements.
Syntax
Following is the syntax for creating an OrderedDictionary −
OrderedDictionary dict = new OrderedDictionary(); dict.Add(key, value); dict[index] = value; // Access by index dict[key] = value; // Access by key
Creating and Populating an OrderedDictionary
Example
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.Remove("E");
Console.WriteLine("Count of elements in OrderedDictionary (Updated)= " + dict.Count);
}
}
The output of the above code is −
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)= 5
Accessing Keys Collection
You can extract and work with the keys collection from an OrderedDictionary −
Example
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
OrderedDictionary dict = new OrderedDictionary();
dict.Add("1", "One");
dict.Add("2", "Two");
dict.Add("3", "Three");
dict.Add("4", "Four");
dict.Add("5", "Five");
dict.Add("6", "Six");
dict.Add("7", "Seven");
dict.Add("8", "Eight");
ICollection col = dict.Keys;
String[] strKeys = new String[dict.Count];
col.CopyTo(strKeys, 0);
Console.WriteLine("Displaying the keys...");
for (int i = 0; i < dict.Count; i++) {
Console.WriteLine(strKeys[i]);
}
}
}
The output of the above code is −
Displaying the keys... 1 2 3 4 5 6 7 8
Index-based Access
OrderedDictionary provides unique functionality to access elements by both key and index −
Example
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
OrderedDictionary dict = new OrderedDictionary();
dict.Add("first", 100);
dict.Add("second", 200);
dict.Add("third", 300);
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]);
dict[1] = 250; // Modify by index
Console.WriteLine("\nAfter modifying index 1:");
Console.WriteLine("second = " + dict["second"]);
}
}
The output of the above code is −
Access by key: first = 100 Access by index: Index 0 = 100 Index 1 = 200 Index 2 = 300 After modifying index 1: second = 250
Common Methods
| Method | Description |
|---|---|
Add(key, value) |
Adds a key-value pair to the end |
Remove(key) |
Removes the entry with the specified key |
RemoveAt(index) |
Removes the entry at the specified index |
Insert(index, key, value) |
Inserts an entry at the specified index |
Clear() |
Removes all entries from the collection |
Conclusion
OrderedDictionary in C# provides a hybrid collection that maintains insertion order while offering both key-based and index-based access. It's ideal when you need dictionary functionality with predictable element ordering, combining the benefits of both dictionaries and lists.
