HashTable的一点儿常识

HashTable 是根据散列算法存储起来的包含名-值对的一种数据存储结构。

HashTable里面存的key和value是Object,所以当我们直接调用HashTable的Values的时候,在控制台输出来只是

Console.WriteLine(hash.Values);

所以要使用HashTable里面的值,最简单的方法就是:

foreach (object sb in hash.Values)
            {
                Console.WriteLine(sb.ToString());
                
            }

Console.WriteLine(hash[key]);

除此之外,Dictionary<T1, T2>在存值这方面也和HashTable一样。

除了以上自定义对象可以遍历HashTable的值以外,也可以用string、DictionaryEntry对象和IDictionaryEnumator接口对象来遍历HashTable。

使用DictionaryEntry

foreach (DictionaryEntry ds  in  hash)
            {
                Console.WriteLine(ds.Value);
            }

使用IDictionaryEnumator接口对象

IDictionaryEnumerator ie = hash.GetEnumerator();
            while (ie.MoveNext())
            {
                Console.WriteLine(ie.Value);
            }
            Console.ReadKey();
原文地址:https://www.cnblogs.com/JhoneLee/p/3069005.html