public GetEnumerator ( ) : IDictionaryEnumerator | ||
return | IDictionaryEnumerator |
Hashtable ht = new Hashtable(); ht.Add("key1", "value1"); ht.Add("key2", "value2"); ht.Add("key3", "value3"); IEnumerator ienum = ht.GetEnumerator(); while (ienum.MoveNext()) { Console.WriteLine(ienum.Key + " : " + ienum.Value); }
Hashtable ht = new Hashtable(); ht.Add("key1", "value1"); ht.Add("key2", "value2"); ht.Add("key3", "value3"); IEnumerator ienum = ht.GetEnumerator(); for (int i = 0; i < ht.Count; i++) { ienum.MoveNext(); Console.WriteLine(ienum.Key + " : " + ienum.Value); }In both examples, we first create a Hashtable object and add some key-value pairs to it. Then we get an enumerator object using the GetEnumerator method and use it to iterate over the key-value pairs in the collection. We can either use a foreach loop or a for loop to iterate over the collection. Package Library: The package library used in these examples is System.Collections, which is part of the .NET framework.