从Hashtable中随机取出一个key,代码如下:
static void Main(string[] args)
{
Hashtable ht = new Hashtable();
ht.Add("key1", "value1");
ht.Add("key2", "value2");
ht.Add("key3", "value3");
//ArrayList list = new ArrayList(ht.Keys);
ArrayList list = new ArrayList(); //储存Hashtable的key集合
foreach (string key in ht.Keys)
{
list.Add(key);
}
int keyIndex = new Random().Next(0, list.Count);
Console.WriteLine(list[keyIndex]); //随机的取出 一个key
Console.ReadKey();
}