Hashtable myTable = new Hashtable();
            myTable.Add("name", "score");
            myTable.Add("张三",50);
            myTable.Add("李四",90);
            myTable.Add("王五",60);
            foreach (DictionaryEntry i in myTable)
            {
                Console.WriteLine(i.Key+"\t"+i.Value);
            }
            Console.ReadLine();
为什么输出的结果不是按照一定的顺序排列的,看起来很混乱!
张三  50
name score
李四  90
王五  60
我想用这种方法达到的效果是:
name score
张三  50
李四  90
王五  60
不用console.Writeline()

解决方案 »

  1.   

    Dictionnary或者其泛型结构的Dictionary<K,T>
    要想排序用SortedList
      

  2.   

    例如:Dictionary<string,int> dctStuInfo=new Dictionary<string,int>;
    dctStuInfo.Add("name", "score"); 
    dctStuInfo.Add("张三",50); 
    dctStuInfo.Add("李四",90); 
    dctStuInfo.Add("王五",60); 
    foreach(KeyValuePair<string,int> kvpItem in dctStuInfo)
    {
    Console.WriteLine(kvpItem.Key+"\t"+kvpItem .Value); 
    }
      

  3.   

    SortedList myTable = new SortedList(); 
                myTable.Add("name", "score"); 
                myTable.Add("张三",50); 
                myTable.Add("李四",90); 
                myTable.Add("王五",60); 
                foreach (DictionaryEntry i in myTable) 
                { 
                    Console.WriteLine(i.Key+"\t"+i.Value); 
                } 
                Console.ReadLine(); 
      

  4.   

    看来错了,<string,int> --->>> <string,object>吧,没注意你的数据类型。手工临时写的,没有测试
      

  5.   

    如果一定要用Hasttable,可以使用Linq,这是VS2008新推出的,代码如下:
    var query = from DictionaryEntry h in myTable orderby h.Key select h;
    foreach(DictionaryEntry de in query)
    Console.WriteLine(de.Key + " " + de.Value);
      

  6.   

    Hashtable   没有顺序的 用 Dictionary <>for 循环下 就可以了