键值对是用Hashtable,还是Liat<>。求键值对使用实例。
功能描述:
1、添加键值对
2、删除指定键值对
3、修改指定键值对
4、根据键来读取值

解决方案 »

  1.   

    向Hashtable键值对集合中添加键值时,键在内部的存储并没有按照添加的先后顺序排列,所以循环foreach读取键的集合时,不会按先后顺序显示出来。实现IDictionary接口的几种类型的集合,似乎都不行,SortedList虽然可以排序,但也不能按添加的先后顺序显示。如果我们需要将字典集合中的键按顺序显示出来,这时我们可以简单的扩展下DictionaryBase基类,就可以满足要求。实现思路很简单,就是在内部加了个ArrayList,用来保存每个Key,ArrayList可以按顺序显示的。/// <summary>
     /// SortedDictionary 的摘要说明。
     /// </summary>
     public class SortedDictionary : DictionaryBase
     {
      ArrayList list;  public SortedDictionary() {
       list = new ArrayList();
      }
       
      public String this[ String key ]  {
       get  {
        return( (String) Dictionary[key] );
       }
       set  {
        if( !this.list.Contains( key ) )
         this.list.Add( key );
        Dictionary[key] = value;
       }
      }  public ICollection Keys  {
       get  {
        return( this.list );
       }
      }  public ICollection Values  {
       get  {
        return( Dictionary.Values );
       }
      }  public void Add( String key, String value )  {
       Dictionary.Add( key, value );
      }  public bool Contains( String key )  {
       return( Dictionary.Contains( key ) );
      }  public void Remove( String key )  {
       Dictionary.Remove( key );
      }  protected override void OnInsert(object key, object value) {
       this.list.Add( key );
      }  protected override void OnRemove(object key, object value) {
       this.list.Remove( key );
      
      

  2.   

    1.字典中的每个元素具备两个属性:键和值。2.键值通过“键”排序,并可以按照键和索引访问(无说明时,默认索引从0开始),键在集合中唯一,值则可以是任意类型数据。3.c#中字典的对象有两个:SortedList和Hashtableeg:SortedList mylist=new SortedList();
         mylist.Add("key1","benjing");     Hashtable mytable=new Hashtable()
         mytable.Add("key1","shandong");4.其中SortedList为可排序的字典,当添加元素时,元素按照正确的排序顺序插入SortedList,同时索引自动进行相应的调整,移除元素亦然。using System;
    using System.Collections;
    class Class1
    {
        static void Main()
          {
            Hashtable mHash = new Hashtable();
            SortedList mSort = new SortedList();
            for (int i = 0; i < 15; i++)
             {
                string oKey = "Key " + i.ToString("D4");
                string oValue = "Value " + i.ToString("x4");
                mHash.Add(oKey, oValue);
                mSort.Add("Key " + i.ToString("D2"), "Val " + i.ToString("X2"));
            }        WrHlist(mHash);
            WriteList(mSort);
            Console.ReadLine();
        }
        public static void WrHlist(Hashtable h)
         {
            IDictionaryEnumerator mIDE = h.GetEnumerator();
            while (mIDE.MoveNext())
             {
                Console.WriteLine("{0}:{1}", mIDE.Key, mIDE.Value);
            }
         }
        public static void WriteList(SortedList mSor)
           {
            Console.WriteLine(" --以下为集合中所包含的对象元素键对!!-- ");
            for (int i = 0; i < 10; i++)
               {
                Console.WriteLine("mSotr[{0}]的值为:{1}", mSor.GetKey(i), mSor["Key " +i]);
              }
           }
    }5.字典的遍历:使用DictionaryEntry对象eg:foreach(DictionaryEntry dic in mylist) //这里的mylist为哈希表初始化并增加键值后的集合。
    {
    label1.text=label2.text+dic.Value.ToString()+dic.Key.ToString();
    }
     
      

  3.   

    Hashtable 属于System.Collections; 命名空间列子上面有了,,键值集合比普通集合的好处就是:因为普通集合只能通过索引访问集合元素,,如果一个集合有很多元素,,在不知道索引的情况下怎么快速的访问到指定元素呢,,,对啦,就用键值对集合Hashtable 集合的每个元素都是一个键/值(key/value)对,,一一对应,,通过键key便可以找到相应的值Hashtable ht=new Hashtable(); 声明一个Hashtable集合假设一个学生类student,里面有name跟sex属性,我们创建一个学生对象student  stu  =new  student();stu.name ="张三";stu.sex="男";ht.Add(stu.name,stu);       //Hashtable 添加元素,第一个key 为stu.name ,第2个value为student对象stu如果有保存了很多同学,,我们要找张三就可以这样  ht["张三"]
      

  4.   

    HashTable lst 
    或 Dictionary<object,object> lst 求键值对使用实例。
    功能描述:
    1、添加键值对      lst[key] = value;
    2、删除指定键值对  lst.Remove()
    3、修改指定键值对  lst[key] = value;
    4、根据键来读取值  lst[Key]如果类型确定,用 dictionary<>最好。
    还有个SortList ,是数组和dictionary的组合,也可以用。
      

  5.   

    http://topic.csdn.net/u/20110410/21/080592cb-ab79-443f-b30b-6f75263e835c.html?seed=1997097151&r=72683935#r_72683935
      

  6.   

    Dictionary,这种键值相对安全,效率也相对较好。