标签:
由于Hashtable内部自带有排序(根据Key的HashCode来进行的),因此有时在使用Hashtable时就会造成数据顺序不可控的情况,有两种办法可以解决,
测试代码:
 Dictionary<string,string> ht=new Dictionary<string, string>(); ht.Add("http://www.sina.com.cn","");
        ht.Add("http://www.sina.com.cn",""); ht.Add("http://www.bjut.edu.cn","");
        ht.Add("http://www.bjut.edu.cn",""); ht.Add("http://lib.bjut.edu.cn", "");
        ht.Add("http://lib.bjut.edu.cn", ""); ht.Add("http://news.bjut.edu.cn", "");
        ht.Add("http://news.bjut.edu.cn", ""); ht.Add("http://sse.bjut.edu.cn", "");
        ht.Add("http://sse.bjut.edu.cn", ""); ht.Add("http://lexus.cnblogs.com", "");
        ht.Add("http://lexus.cnblogs.com", ""); ht.Add("http://www.sina.com.cn/sport", "");
        ht.Add("http://www.sina.com.cn/sport", ""); ht.Add("http://www.sina.com.cn/ent", "");
        ht.Add("http://www.sina.com.cn/ent", "");
 foreach(var kvp in ht)
        foreach(var kvp in ht) Console.WriteLine(kvp.Key);
            Console.WriteLine(kvp.Key); Console.WriteLine("============================================");
        Console.WriteLine("============================================"); Hashtable ht2=new Hashtable();
        Hashtable ht2=new Hashtable(); ht2.Add("http://www.sina.com.cn", "");
        ht2.Add("http://www.sina.com.cn", ""); ht2.Add("http://www.bjut.edu.cn", "");
        ht2.Add("http://www.bjut.edu.cn", ""); ht2.Add("http://lib.bjut.edu.cn", "");
        ht2.Add("http://lib.bjut.edu.cn", ""); ht2.Add("http://news.bjut.edu.cn", "");
        ht2.Add("http://news.bjut.edu.cn", ""); ht2.Add("http://sse.bjut.edu.cn", "");
        ht2.Add("http://sse.bjut.edu.cn", ""); ht2.Add("http://lexus.cnblogs.com", "");
        ht2.Add("http://lexus.cnblogs.com", ""); ht2.Add("http://www.sina.com.cn/sport", "");
        ht2.Add("http://www.sina.com.cn/sport", ""); ht2.Add("http://www.sina.com.cn/ent", "");
        ht2.Add("http://www.sina.com.cn/ent", ""); foreach(DictionaryEntry i in ht2)
        foreach(DictionaryEntry i in ht2) Console.WriteLine(i.Key);
            Console.WriteLine(i.Key);
第一种是继承Hashtable,自己创建一个新的类,用一个ArrayList对象保存keys;
代码:(转)
using System; using System.Collections;
using System.Collections;
 namespace NoSortHashtable
namespace NoSortHashtable {
{ /// <summary>
    /// <summary> /// Summary description for NoSortedHashtable.
    /// Summary description for NoSortedHashtable. /// </summary>
    /// </summary> public class NoSortHashtable : Hashtable
    public class NoSortHashtable : Hashtable {
    { private ArrayList keys = new ArrayList();
        private ArrayList keys = new ArrayList();
 public NoSortHashtable()
        public NoSortHashtable() {
        { }
        } 
        
 public override void Add(object key, object value)
        public override void Add(object key, object value) {
        { base.Add (key, value);
            base.Add (key, value); keys.Add (key);
            keys.Add (key); }
        }
 public override ICollection Keys
        public override ICollection Keys {
        { get
            get {
            { return keys;
                return keys; }
            } }
        }
 public override void Clear()
        public override void Clear() {
        { base.Clear ();
            base.Clear (); keys.Clear ();
            keys.Clear (); }
        }
 public override void Remove(object key)
        public override void Remove(object key) {
        { base.Remove (key);
            base.Remove (key); keys.Remove    (key);
            keys.Remove    (key); }
        } public override IDictionaryEnumerator GetEnumerator()
        public override IDictionaryEnumerator GetEnumerator() {
        { return base.GetEnumerator ();
            return base.GetEnumerator (); }
        }
 }
    } }
}
测试:
----------------------------------------------------------------------
第二种办法是采用泛型的Dictionary<T,K>对象,该对象按照插入的顺序输出;
         Dictionary<string,string> ht=new Dictionary<string, string>(); ht.Add("http://www.sina.com.cn","");
        ht.Add("http://www.sina.com.cn",""); ht.Add("http://www.bjut.edu.cn","");
        ht.Add("http://www.bjut.edu.cn",""); ht.Add("http://lib.bjut.edu.cn", "");
        ht.Add("http://lib.bjut.edu.cn", ""); ht.Add("http://news.bjut.edu.cn", "");
        ht.Add("http://news.bjut.edu.cn", ""); ht.Add("http://sse.bjut.edu.cn", "");
        ht.Add("http://sse.bjut.edu.cn", ""); ht.Add("http://lexus.cnblogs.com", "");
        ht.Add("http://lexus.cnblogs.com", ""); ht.Add("http://www.sina.com.cn/sport", "");
        ht.Add("http://www.sina.com.cn/sport", ""); ht.Add("http://www.sina.com.cn/ent", "");
        ht.Add("http://www.sina.com.cn/ent", "");
          foreach(var kvp in ht) Console.WriteLine(kvp.Key);
              Console.WriteLine(kvp.Key);
标签:
原文地址:http://www.cnblogs.com/coolsundy/p/4202554.html