C# | Get an IDictionaryEnumerator object in OrderedDictionary Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report OrderedDictionary.GetEnumerator method returns an IDictionaryEnumerator object that iterates through the OrderedDictionary collection. Syntax: public virtual System.Collections.IDictionaryEnumerator GetEnumerator (); Return Value: An IDictionaryEnumerator object for the OrderedDictionary collection. Below given are some examples to understand the implementation in a better way: Example 1: CSHARP // C# code to get an IDictionaryEnumerator // object that iterates through the // OrderedDictionary collection. 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 an IDictionaryEnumerator object // that iterates through the OrderedDictionary // collection. IDictionaryEnumerator myEnumerator = myDict.GetEnumerator(); while (myEnumerator.MoveNext()) { Console.WriteLine(myEnumerator.Key + " --> " + myEnumerator.Value); } } } Output: key1 --> value1 key2 --> value2 key3 --> value3 key4 --> value4 key5 --> value5 Example 2: CSHARP // C# code to get an IDictionaryEnumerator // object that iterates through the // OrderedDictionary collection. 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 an IDictionaryEnumerator object // that iterates through the OrderedDictionary // collection. IDictionaryEnumerator myEnumerator = myDict.GetEnumerator(); while (myEnumerator.MoveNext()) { Console.WriteLine(myEnumerator.Key + " --> " + myEnumerator.Value); } } } Output: A --> Apple B --> Banana C --> Cat D --> Dog Note: Enumerators can be used to read the data in the collection, but they cannot be used to modify the underlying collection. Initially, the enumerator is positioned before the first element in the collection. This method is an O(1) operation. An enumerator remains valid as long as the collection remains unchanged. If changes are made to the collection, such as adding, modifying, or deleting elements, the enumerator is irrecoverably invalidated and its behavior is undefined. Reference: https://learn.microsoft.com/en-us/dotnet/api/system.collections.specialized.ordereddictionary.getenumerator?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Get an ICollection containing values in OrderedDictionary S Sahil_Bansall Follow Improve Article Tags : C# CSharp-method CSharp-Specialized-Namespace CSharp-Specialized-OrderedDictionary Similar Reads C# | Check if two OrderedDictionary objects are equal Equals(Object) Method which is inherited from the Object class is used to check if a specified OrderedDictionary object is equal to another OrderedDictionary object or not. Syntax: public virtual bool Equals (object obj); Here, obj is the object which is to be compared with the current object. Retur 2 min read How to create an OrderedDictionary in C# OrderedDictionary() constructor is used to initialize a new instance of the OrderedDictionary class which will be empty and will have the default initial capacity. OrderedDictionary Class represents a collection of key/value pairs that are accessible by the key or index. It is present in System.Coll 2 min read C# | Get an ICollection containing keys in OrderedDictionary OrderedDictionary.Keys property is used to get an ICollection object containing the keys in the OrderedDictionary collection. Syntax: public System.Collections.ICollection Keys { get; } Return Value: It returns an ICollection object containing the keys in the OrderedDictionary collection. Below give 2 min read C# | OrderedDictionary.Item[Object] Property There are two methods in the overload list of this property as follows: Item[Int32] : Used to get or set the value at the specified index.Item[Object] : Used to get or set the value with the specified key. OrderedDictionary.Item[Int32] Property This property is used to get or set the value at the sp 3 min read C# | Get an ICollection containing values in OrderedDictionary OrderedDictionary.Values property is used to get an ICollection object containing the values in the OrderedDictionary collection. Syntax: public System.Collections.ICollection Values { get; } Return Value: It returns an ICollection object containing the values in the OrderedDictionary collection. Be 2 min read Check if two SortedDictionary objects are equal in C# Equals(Object) Method which is inherited from the Object class is used to check if a specified SortedDictionary object is equal to another SortedDictionary object or not. Syntax: public virtual bool Equals (object obj); Here, obj is the object which is to be compared with the current object. Return 2 min read Like