码迷,mamicode.com
首页 > 其他好文 > 详细

HashTable集合遍历的三种方法

时间:2017-06-22 23:03:37      阅读:287      评论:0      收藏:0      [点我收藏+]

标签:--   ==   示例   ash   new   方法   string   item   ring   

hashtable集合遍历可以根据key,value以及key+value

示例代码:

Hashtable table = new Hashtable();

Student stu = new Student();
stu.Name = "李四";
stu.Age = 18;

Student stu1 = new Student();
stu1.Name = "张三";
stu1.Age = 18;

Student stu2 = new Student();
stu2.Name = "张飞";
stu2.Age = 18;

table.Add(stu.Name, stu);
table.Add(stu1.Name, stu);
table.Add(stu2.Name, stu);

//HashTable集合遍历的三种方法
//方法一: 根据key值进行遍历
foreach (string item in table.Keys)
{
Console.WriteLine("key为" + item);
Student stu4 = (Student)table[item];
Console.WriteLine(stu4.Name);

}

Console.WriteLine("===================================");
//方法二: 根据values值进行遍历
foreach (Student student in table.Values)
{
Console.WriteLine(student.Name);
}
Console.WriteLine("---------------------------------");
//方法三:根据values和key进行遍历
foreach (DictionaryEntry item in table)
{
Console.WriteLine("key是{0}\tvalue是{1}", item.Key, ((Student)item.Value).Name);

}

 

HashTable集合遍历的三种方法

标签:--   ==   示例   ash   new   方法   string   item   ring   

原文地址:http://www.cnblogs.com/sujulin/p/6958539.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!