在C#中用HASHTABLE搜索是否有相同的名字。如何写啊。

解决方案 »

  1.   

    什么意思?没有看懂!!!
    hashtable是一种存储结构,怎么能用来搜索??
    你是不是想说把结果放到 hashtable中然后判断有没有重复的??
    但是如果你能取出来放到hashtable中干吗还用hashtable啊 
      

  2.   

    看是否满足楼主的要求
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Text;
    namespace JustTry
    {
        class Program
        {
            static void Main(string[] args)
            {
                Hashtable ht = new Hashtable();
                ht.Add("0","张三");
                ht.Add("1", "李四");
                ht.Add("2", "王老五");
                ht.Add("3", "张三");
                ValidateSaveValue(ht);
                Console.ReadLine();
            }        public static void ValidateSaveValue(Hashtable ht) 
            {
                foreach (DictionaryEntry de1 in ht)
                {
                    foreach (DictionaryEntry de2 in ht)
                    {
                        if (de1.Value == de2.Value && de1.Key != de2.Key)
                        {
                            Console.WriteLine("有同名");
                            return;
                        }
                    }
                }
            }
        }
    }
      

  3.   

    不要用hashtable了,用IDictionary<TKey,TValue>吧,有方法ContainsKey()可以判断是否存在相同的Key,并且比Hashtable效率高呀
      

  4.   

    hashtable好像不能用相同的名字哦key
    你说的是value吗?上网上搜下hashtable的遍历就可以啊
      

  5.   

    推荐使用泛型IDictionary  <TKey,TValue>
    有contain方法提供,很好用的,判断是否包含
      

  6.   

    hashtable也有ContainsKey()方法来判断是否存在相同的键或值。详细查看MSDN文档
      

  7.   

    但Hashtable需要拆/装箱的操作,如果要求不高可以用,如果要求效率的话,用泛型吧,这么好的东西不用那用什么?
      

  8.   

    就用 Hashtable.Contains(key); 这个方法就行
      

  9.   

    好象有一个hashtable的字符串版本的
    System.Collections.Specialized.StringDictionary吧.
      

  10.   

    可以用for循环遍历hashtable吗