/*
我們查詢資料得知Dictionary的遍歷順序和添加Add時的順序是一致的,不像 HashTable 順序不可知;於是我要依賴Dictionary的這種順序一致特性做一個,固定大小400長度的隊列,如果超過400個元素就Remove刪除掉老的數 據,保留新的數據;我為什么不用隊列Queue因為它沒有ContainsKey鍵查找功能,因為我需要用鍵來去重.
當我的程序運行一段時間后發生邏輯問題 ,本來我的程序邏輯是超是400后刪除Remove最添加的老數據,但是刪除的卻是最后添加的新數據,當遍歷的時候順序打亂了,並不是添加的程序,經測試發現字典對象並不能一定保持程序一致,一個使用Remove順序一致性將得不到保證,經測試
*/
SortedDictionary<string, string> testdict = new SortedDictionary<string, string>(); testdict.Add("a", "a"); testdict.Add("b", "b"); testdict.Add("c", "c"); testdict.Remove("a"); testdict.Add("a", "a"); foreach (KeyValuePair<string, string> kv in testdict) { Response.Write(kv.Key);//輸出abc 而不是 bca } Dictionary<string, string> testdict = new Dictionary<string, string>(); testdict.Add("a", "a"); testdict.Add("b", "b"); testdict.Add("c", "c"); testdict.Remove("a"); testdict.Add("a", "a"); foreach (KeyValuePair<string, string> kv in testdict) { Response.Write(kv.Key);//輸出abc 而不是 bca }
一但使用了Remove字典對象的程序,它已經亂了,現在只有依賴字典加對象雙結合才能達到我的要求,悲劇