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