Listnumbers = new List { 1, 2, 3, 4, 5 }; IEnumerator enumerator = numbers.GetEnumerator(); while (enumerator.MoveNext()) { int currentNumber = enumerator.Current; Console.WriteLine(currentNumber); }
DictionaryIn this example, we create a dictionary and obtain an enumerator for it. The `while` loop iterates over the collection using the `MoveNext()` method to move the iterator to the next item and the `Current` property to get the current key-value pair. Package library: System.Collections.Generic.dict = new Dictionary { { "apple", 1 }, { "banana", 2 }, { "cherry", 3 } }; IEnumerator > enumerator = dict.GetEnumerator(); while (enumerator.MoveNext()) { KeyValuePair pair = enumerator.Current; Console.WriteLine($"{pair.Key}: {pair.Value}"); }