.net框架-字典对象 Hashtable & Dictionary<TKey,TValue> & SortedList

字典对象:

  字典对象是表示键值对的集合

  字典对象有Hashtable(.net 1.0)及其泛型版本Dictionary<TKey,TValue>

  字典对象还包括SortedList及其泛型版本SortedList<TKey,TValue>(SortedList按键进行排序)

  字典对象实现了接口:IDictionary, ICollection, IEnumerable, ICloneable (泛型实现了IDictionary, ICollection, IEnumerable对应的泛型接口)

 

IDictionary接口:

  object this[object key] {get;set;}  

  ICollection Keys {get;set;}

  ICollection Values {get;set;}

  void Add(object key, object value)

  void Remove (object key)

  object Contains(object key)

 

IDictionary<TKey, TValue>接口:

  (1)与IDictionary相同的:

    TValue this[TKey] {get;set;}  

    void Add(TKey,TValue)

    .........  (其它同IDictionary接口略,其它形式均为泛型T占位符参数代替Object参数)

  (2)与IDictionary不同的:

    bool ContainsKey(TKey)

    bool TryGetValue(TKey, out TValue value)

 

区别:

  Hastable 和 Dictionary<TKey,TValue>,前者是.net1.0下的字典对象,后者是前者泛型版本 ,泛型减少了装、拆箱和强制转换,效率更高

  SortedList 和 SortedList<TKey,TValue>,按键序存储的字典对象

  Hastable和SortedList,前进自然存储、后者按键序存储,故前者插入快,查找慢,后者反之。

  

参考:

  http://www.cnblogs.com/xqhppt/archive/2011/09/15/2178101.html

 

posted @ 2017-02-09 14:55  水手哥  阅读(145)  评论(0编辑  收藏  举报