我需要一个多级的索引,所以希望第一级索引是(key1,Hashtable),然后第二级是(key2,Hashtable),第三级是(key3,string),即:
Hashtable tb = new Hashtable();tb.Add("key1", new Hashtable())
tb["key1"].Add("key2", new Hashtable());
tb["key1"]["key2"].Add("key3", "sss");但是编译通不过,即使我在添加操作前面加上了类型强制转化都不行,哪位帮忙看一下?如果Hashtable不能满足这种要求,c#中有没有其他的类可以达到要求?

解决方案 »

  1.   

    Dictionary<(Of <(TKey, TValue>)>) 类 
      

  2.   

    可以的啊:            Hashtable ht = new Hashtable();
                Hashtable ht2 = new Hashtable();
                ht2.Add("ht2key1", "ht2value1");
                ht2.Add("ht2key2", "ht2value2");
                ht.Add("htkey1", ht2);
                ((Hashtable)ht["htkey1"]).Add("ht2key3", "ht2value3");
    不过还是推荐使用Dictionary,毕竟泛型是类型安全的
      

  3.   

               Hashtable tb = new Hashtable();
                tb.Add("key1", new Hashtable());
                (tb["key1"] as Hashtable).Add("key2", new Hashtable());
                ((tb["key1"] as Hashtable)["key2"] as Hashtable).Add("key3", "sss");
      

  4.   

    用Dictionary<TKey,TValue>,推荐推荐~~~
      

  5.   

                Hashtable ht = new Hashtable();
                Hashtable ht2 = new Hashtable();
                ht2.Add("ht2key1", "ht2value1");
                ht2.Add("ht2key2", "ht2value2");
                ht.Add("htkey1", ht2);
                ((Hashtable)ht["htkey1"]).Add("ht2key3", "ht2value3");
    这样就ok了