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.
Properties of OrderedDictionary Class :
- Each element is a key/value pair stored in a DictionaryEntry object.
- A key cannot be null, but a value can be.
- The elements of an OrderedDictionary are not sorted by the key.
- Elements can be accessed either by the key or by the index.
Constructors
| Constructor | 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. |
Example:
// C# code to create a OrderedDictionary using System; using System.Collections; using System.Collections.Specialized; class GFG { // Driver method public static void Main() { // Creating a orderedDictionary named myDict OrderedDictionary myDict = new OrderedDictionary(); // Adding key and value in myDict myDict.Add("1", "ONE"); myDict.Add("2", "TWO"); myDict.Add("3", "THREE"); // Displaying the number of key/value // pairs in myDict Console.WriteLine(myDict.Count); // Displaying the key/value pairs in myDict foreach(DictionaryEntry de in myDict) Console.WriteLine(de.Key + " --> " + de.Value); } } |
chevron_right
filter_none
Output:
3 1 --> ONE 2 --> TWO 3 --> THREE
Properties
| Property | 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:
// C# code to check if OrderedDictionary // collection is read-only using System; using System.Collections; using System.Collections.Specialized; class GFG { // Driver method public static void Main() { // Creating a orderedDictionary named myDict OrderedDictionary myDict = new OrderedDictionary(); // Adding key and value in myDict myDict.Add("key1", "value1"); myDict.Add("key2", "value2"); myDict.Add("key3", "value3"); myDict.Add("key4", "value4"); myDict.Add("key5", "value5"); // Checking if OrderedDictionary // collection is read-only Console.WriteLine(myDict.IsReadOnly); } } |
chevron_right
filter_none
Output:
False
Example 2:
// C# code to get the number of // key/values pairs contained // in the OrderedDictionary using System; using System.Collections; using System.Collections.Specialized; class GFG { // Driver method public static void Main() { // Creating a orderedDictionary named myDict OrderedDictionary myDict = new OrderedDictionary(); // Adding key and value in myDict myDict.Add("A", "Apple"); myDict.Add("B", "Banana"); myDict.Add("C", "Cat"); myDict.Add("D", "Dog"); // To Get the number of key/values // pairs contained in the OrderedDictionary Console.WriteLine("Number of key/value pairs are : " + myDict.Count); } } |
chevron_right
filter_none
Output:
Number of key/value pairs are : 4
Methods
| Method | 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:
// C# code to get a read-only // copy of the OrderedDictionary using System; using System.Collections; using System.Collections.Specialized; class GFG { // Driver method public static void Main() { // Creating a orderedDictionary named myDict OrderedDictionary myDict = new OrderedDictionary(); // Adding key and value in myDict myDict.Add("key1", "value1"); myDict.Add("key2", "value2"); myDict.Add("key3", "value3"); myDict.Add("key4", "value4"); myDict.Add("key5", "value5"); // To Get a read-only copy of // the OrderedDictionary OrderedDictionary myDict_1 = myDict.AsReadOnly(); // Checking if myDict_1 is read-only Console.WriteLine(myDict_1.IsReadOnly); } } |
chevron_right
filter_none
Output:
True
Example 2:
// C# code to remove the entry // with the specified key from // the OrderedDictionary using System; using System.Collections; using System.Collections.Specialized; class GFG { // Driver method public static void Main() { // Creating a orderedDictionary named myDict OrderedDictionary myDict = new OrderedDictionary(); // Adding key and value in myDict myDict.Add("key1", "value1"); myDict.Add("key2", "value2"); myDict.Add("key3", "value3"); myDict.Add("key4", "value4"); myDict.Add("key5", "value5"); // Displaying the number of element initially Console.WriteLine("Number of elements are : " + myDict.Count); // Displaying the elements in myDict foreach(DictionaryEntry de in myDict) Console.WriteLine(de.Key + " -- " + de.Value); // Removing the entry with the specified // key from the OrderedDictionary myDict.Remove("key2"); // Displaying the number of element initially Console.WriteLine("Number of elements are : " + myDict.Count); // Displaying the elements in myDict foreach(DictionaryEntry de in myDict) Console.WriteLine(de.Key + " -- " + de.Value); } } |
chevron_right
filter_none
Output:
Number of elements are : 5 key1 -- value1 key2 -- value2 key3 -- value3 key4 -- value4 key5 -- value5 Number of elements are : 4 key1 -- value1 key3 -- value3 key4 -- value4 key5 -- value5
Reference:
Recommended Posts:
- C# | Add key and value into OrderedDictionary collection
- C# | Check if OrderedDictionary collection contains a specific key
- C# | Check if OrderedDictionary collection is read-only
- C# | Get a read-only copy of the OrderedDictionary
- C# | OrderedDictionary.Item[Object] Property
- C# | Remove all elements from OrderedDictionary
- C# | Get the number of key/values pairs contained in OrderedDictionary
- C# | Remove entry with specified key from OrderedDictionary
- C# | Get an IDictionaryEnumerator object in OrderedDictionary
- C# | Get an ICollection containing values in OrderedDictionary
- C# | Get an ICollection containing keys in OrderedDictionary
- C# | Insert into OrderedDictionary with key and value at specified index
- C# | Remove the entry at specified index from OrderedDictionary
- C# | Copy OrderedDictionary elements to Array instance at the specified index
- C# | Insert a new entry in OrderedDictionary with specified key and value
- C# | Check if two OrderedDictionary objects are equal
- How to create an OrderedDictionary in C#
- C# | How to get TypeCode for the class String
- C# | Static Class
- C# | Class and Object
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

