Last Updated : 11 Jul, 2025
In C#, the 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.
Example: This example demonstrates how to use the OrderedDictionary class to store and iterate over key-value pairs while maintaining the order in which the elements are added.
C#
// C# program to demonstrates the
// working of OrderedDictionary
using System;
using System.Collections.Specialized;
using System.Collections;
class Geeks
{
static void Main()
{
// Create an OrderedDictionary instance
OrderedDictionary od = new OrderedDictionary();
// Add key-value pairs
od.Add("Geek1", 1);
od.Add("Geek2", 2);
od.Add("Geek3", 3);
od.Add("Geek4", 4);
// Iterate through the OrderedDictionary
// and print the key-value pairs
foreach (DictionaryEntry i in od)
{
Console.WriteLine($"{i.Key}: {i.Value}");
}
}
}
Geek1: 1 Geek2: 2 Geek3: 3 Geek4: 4Declaration of OrderedDictionary
In C#, the orderedDicitonary class is represented as:
Constructors Constructors Description OrderedDictionary() Initializes a new instance of the OrderedDictionary class. OrderedDictionary(IEqualityComparer) Initializes a new instance of the OrderedDictionary class using the specified comparer. OrderedDictionary(Int32) Initializes a new instance of the OrderedDictionary class using the specified initial capacity. OrderedDictionary(Int32, IEqualityComparer) Initializes a new instance of the OrderedDictionary class using the specified initial capacity and comparer. OrderedDictionary(SerializationInfo, StreamingContext) Initializes a new instance of the OrderedDictionary class that is serializable using the specified SerializationInfo and StreamingContext objects.OrderedDictionary orderedDict = new OrderedDictionary();
Example: This example demonstrates how to create an OrderedDictionary, add key-vlaue pairs and display the count and key-value while maintaining the insertion order.
C#
// C# program to demosntrate the count and
// key-value pair of OrderedDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
class Geeks {
// Driver method
public static void Main()
{
// Creating a orderedDictionary named myDict
OrderedDictionary od = new OrderedDictionary();
// Adding key and value in myDict
od.Add("1", "ONE");
od.Add("2", "TWO");
od.Add("3", "THREE");
// Displaying the number of key/value
// pairs in myDict
Console.WriteLine("The count is : " + od.Count);
// Displaying the key/value pairs in myDict
foreach(DictionaryEntry i in od)
Console.WriteLine(i.Key + " --> " + i.Value);
}
}
The count is : 3 1 --> ONE 2 --> TWO 3 --> THREEProperties
The OrderedDictionary class provides several properties to access its state.
Properties Description Count Gets the number of key/values 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.Example 1: This example checks if the dictionary is read-only using the IsReadOnly property.
C#
// C# code to check if OrderedDictionary
// collection is read-only
using System;
using System.Collections;
using System.Collections.Specialized;
class Geeks {
public static void Main()
{
// Creating a orderedDictionary named myDict
OrderedDictionary od = new OrderedDictionary();
// Adding key and value in myDict
od.Add("key1", "value1");
od.Add("key2", "value2");
od.Add("key3", "value3");
// Checking if OrderedDictionary
// collection is read-only
Console.WriteLine(od.IsReadOnly);
}
}
Example 2: This example demonstrates the count of key-value pairs using the Count property.
C#
// C# code to get the number of
// key/values pairs contained
// in the OrderedDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
class Geeks {
public static void Main()
{
// Creating a orderedDictionary named myDict
OrderedDictionary od = new OrderedDictionary();
// Adding key and value in myDict
od.Add("1", "Geeks");
od.Add("2", "for");
od.Add("3", "Geeks");
od.Add("4", "C#");
// To Get the number of key/values
// pairs contained in the OrderedDictionary
Console.WriteLine("Number of key-value pairs are : "
+ od.Count);
}
}
Number of key-value pairs are : 4Methods Methods Description Add(Object, Object) Adds an entry with the specified key and value into the OrderedDictionary collection with the lowest available index. AsReadOnly() Returns a read-only copy of the current OrderedDictionary collection. Clear() Removes all elements from the OrderedDictionary collection. Contains(Object) Determines whether the OrderedDictionary collection contains a specific key. CopyTo(Array, Int32) Copies the OrderedDictionary elements to a one-dimensional Array object at the specified index. Equals(Object) Determines whether the specified object is equal to the current object. GetEnumerator() Returns an IDictionaryEnumerator object that iterates through the OrderedDictionary collection. GetHashCode() Serves as the default hash function. GetObjectData(SerializationInfo, StreamingContext) Implements the ISerializable interface and returns the data needed to serialize the OrderedDictionary collection. GetType() Gets the Type of the current instance. Insert(Int32, Object, Object) Inserts a new entry into the OrderedDictionary collection with the specified key and value at the specified index. MemberwiseClone() Creates a shallow copy of the current Object. OnDeserialization(Object) Implements the ISerializable interface and is called back by the deserialization event when deserialization is complete. Remove(Object) Removes the entry with the specified key from the OrderedDictionary collection. RemoveAt(Int32) Removes the entry at the specified index from the OrderedDictionary collection. ToString() Returns a string that represents the current object.
Example 1: This example demonstrates how to create an OrderedDictionary, add key-value pairs and obtain a read-only copy of the docitonatry using AsReadOnly().
C#
// C# code to get a read-only
// copy of the OrderedDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
class Geeks {
public static void Main()
{
// Creating a orderedDictionary named myDict
OrderedDictionary od = new OrderedDictionary();
// Adding key and value in myDict
od.Add("key1", "value1");
od.Add("key2", "value2");
od.Add("key3", "value3");
// To Get a read-only copy of
// the OrderedDictionary
OrderedDictionary d = od.AsReadOnly();
// Checking if d is read-only
Console.WriteLine(d.IsReadOnly);
}
}
Example 2: This example demonstrates, how to remove an entry and display the updated count and elements.
C#
// C# code to remove the entry
// with the specified key from
// the OrderedDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
class Geeks {
public static void Main()
{
// Creating a orderedDictionary named myDict
OrderedDictionary od = new OrderedDictionary();
// Adding key and value in myDict
od.Add("key1", "value1");
od.Add("key2", "value2");
od.Add("key3", "value3");
// Displaying the number of element initially
Console.WriteLine("Number of elements are : "
+ od.Count);
// Displaying the elements in myDict
foreach(DictionaryEntry i in od)
Console.WriteLine(i.Key + " --> " + i.Value);
// Removing the entry with the specified
// key from the OrderedDictionary
od.Remove("key2");
// Displaying the number of element initially
Console.WriteLine("Number of elements are: "
+ od.Count);
// Displaying the elements in myDict
foreach(DictionaryEntry i in od)
Console.WriteLine(i.Key + " -->" + i.Value);
}
}
Number of elements are : 3 key1 --> value1 key2 --> value2 key3 --> value3 Number of elements are: 2 key1 -->value1 key3 -->value3
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4