var ht = new Hashtable {{"1", "a"}, {"d", "2"}, {"a", "1"}, {"c", "3"}};
        foreach (DictionaryEntry entry in ht)
        {
            Response.Write(string.Format("<font color=red>{0}</font>={1}<br>", entry.Key, entry.Value));
        }为什么结果是
d=2
c=3
1=a
a=1
,请大家主意输出的顺序

解决方案 »

  1.   

    楼主 我测试了下 我的为啥是
    a c d 1
      

  2.   

    不知道啊,我的是win7 64bit .net4.0
      

  3.   

    按道理来说 我的是正确的。我的是2003 vs2008 .net2.0
      

  4.   

    好像HashTable的内部排序机制 怪怪的
      

  5.   

    HashTable is stored by the hashcode of the key, not by the index .
      

  6.   

    本帖最后由 net_lover 于 2012-06-28 13:31:23 编辑
      

  7.   

    HashTable本来就是无序的,要想有序应该用Dictionary<>var ht = new Dictionary<string, string> {{"1", "a"}, {"d", "2"}, {"a", "1"}, {"c", "3"}};
            foreach (KeyValuePair<string,string> entry in ht)
            {
                Response.Write(string.Format("<font color=red>{0}</font>={1}<br>", entry.Key, entry.Value));
            }
      

  8.   

    貌似跟hashtable的内部结构实现有关 
    参考
      

  9.   


            var ht = new Hashtable {{"1", "a"}, {"d", "2"}, {"a", "1"}, {"c", "3"}};
            foreach (DictionaryEntry entry in ht)
            {
                Response.Write(string.Format("<font color=red>{0}</font>={1}-------{2}:{3}---{4}<br>", 
                    entry.Key, entry.Value,entry.Key.GetHashCode(), entry.Value.GetHashCode(),entry.Key.GetHashCode()+entry.Value.GetHashCode()));
            }
    结果
    d=2-------372029370:372029328---744058698
    c=3-------372029375:372029327---744058702
    1=a-------372029325:372029373---744058698
    a=1-------372029373:372029325---744058698我把key和value都GetHashCode,还是看不出来是按照什么顺序啊.
    我就是想了解一下hashtable呵呵
      

  10.   

    hashtable 是根据生成的hash值进行排序的,顺组不是原来的顺序
    一般这种情况用Dictionary来做
      

  11.   

    嗯,哈希表是不排序的,可以用datatable 或者链表等来替代
      

  12.   


    生成的hash值我也列出来了啊,没见按照这个排序啊