【转载】如何在C#中遍历hashtable?
摘要
常有忘而转载之。
正文
foreach (DictionaryEntry de in myHashtable) {...}

例子:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class PGhashtable : System.Web.UI.Page
{
Hashtable hstsore = new Hashtable();

protected void Page_Load(object sender, EventArgs e)
{
hstsore.Add("Jone", 67);
hstsore.Add("Gore", 89);
hstsore.Add("Tiny", 56);
hstsore.Add("Big", 78);
hstsore.Add("Titan", 78);
hstsore.Add("Kery", 89);
hstsore.Add("Bulk", 90);
hstsore.Add("Hulk", 99);
hstsore.Add("Mike", 87);
hstsore.Add("Sery", 95);
int max, min;
max = Convert .ToInt16 ( hstsore["Jone"]);
min =Convert .ToInt16 ( hstsore["Jone"]);
foreach(DictionaryEntry de in hstsore )
{
int temp = Convert.ToInt16(de.Value);
if ( temp >= max)
{
max = temp ;
}
if (temp<=min)
{
min = temp;
}
}
lblgdi.Text = "最高分是:" + max.ToString() + " 最低分是:" + min.ToString();
}
}

例子2:

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace GetSpec
{
class HashTableDemo
{

public string Get()
{
Hashtable hashTable = new Hashtable();
hashTable.Add(1, "wuyi");
hashTable.Add(2, "sky");
System.Windows.Forms.MessageBox.Show((string)hashTable[1]);

foreach (DictionaryEntry de in hashTable)
{
System.Windows.Forms.MessageBox.Show(de.Key.ToString());
System.Windows.Forms.MessageBox.Show(de.Value.ToString());
}

System.Collections.IDictionaryEnumerator enumerator = hashTable.GetEnumerator();
while (enumerator.MoveNext())
{
System.Windows.Forms.MessageBox.Show(enumerator.Key.ToString());
System.Windows.Forms.MessageBox.Show( enumerator.Value.ToString());
}

return (string)hashTable[1];
}
}
}
引用地址:http://www.gogosonny.net/suibi/gogosonny/2009/7/27/2511.aspx
作者 GoGoSonny | 添加 2009-7-27 10:4 | 回复总数 0| 所属类别 代码文章


在下面输入您的回复:
1.请不要夹带某些非水产的关键字。
2.回复内容字符数不要超过600。
3.验证码字母不区分大小写。
4.审核后回复的内容才会被显示。
5.点击图片可更换验证码。