C# Dictionary和HashTable中遍历Keys怎么写?

上一篇 / 下一篇  2009-01-14 12:02:00

比如说建立了一个类Student包含字段name,age,hobby 用name作为Keys Dictionary<string, Student> stu = new Dictionary<string, Student>(); stu.Add(s1.Name,s1); stu.Add(s2.Name,s2); stu.Add(s3.Name,s3); 添加好了数据 遍历Value可以写 foreach (Student ss in stu.Values) { Student ass = (Student)ssss; MessageBox.Show(ass.Name.ToString()); } 那么遍历Keys都时候具体怎么写? foreach (Student ss in stu.Keys) { }

参考答案:foreach (Student ss in stu.Values)
{
//Student ass = (Student)ssss;//这句话没用还是错误的
MessageBox.Show(ss.Name.ToString()); //ass改成ss
}
那么遍历Keys都时候具体怎么写?
//foreach (Student ss in stu.Keys)//这句话是错误的KEYS是string
foreach (string key in stu.Keys) //这样遍历
{
MessageBox.Show(key);
}

}

TAG: dictionary hashtable keys

添加网摘:

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

日历

« 2010-07-13  
    123
45678910
11121314151617
18192021222324
25262728293031

数据统计

  • 访问量: 1716481
  • 日志数: 105489
  • 建立时间: 2008-12-16
  • 更新时间: 2009-01-31

RSS订阅

Open Toolbar