在C#中,可以使用以下几种方法来遍历Hashtable:
使用foreach循环遍历Hashtable中的键值对:Hashtable hashtable = new Hashtable();// 添加键值对到Hashtable中foreach (DictionaryEntry entry in hashtable){ Console.WriteLine("Key: {0}, Value: {1}", entry.Key, entry.Value);}使用GetEnumerator方法和while循环来遍历Hashtable中的键值对:Hashtable hashtable = new Hashtable();// 添加键值对到Hashtable中IEnumerator enumerator = hashtable.GetEnumerator();while (enumerator.MoveNext()){ DictionaryEntry entry = (DictionaryEntry)enumerator.Current; Console.WriteLine("Key: {0}, Value: {1}", entry.Key, entry.Value);}使用Keys和Values属性遍历Hashtable中的键和值:Hashtable hashtable = new Hashtable();// 添加键值对到Hashtable中foreach (var key in hashtable.Keys){ Console.WriteLine("Key: {0}, Value: {1}", key, hashtable[key]);}无论使用哪种方法,都可以轻松地遍历Hashtable中的键值对。选择适合你的场景的方法来遍历Hashtable,以便更高效地处理数据。


