我的代码如下:
Hashtable ht = new Hashtable();
ht.Add("First", "Hello");
ht.Add("Second", "World");
ht.Add("Third", "!");
现在如果我想把这个Hashtable的第一个数据取出来,要如何做才可以取出来呢?
Hashtable ht = new Hashtable();
ht.Add("First", "Hello");
ht.Add("Second", "World");
ht.Add("Third", "!");
现在如果我想把这个Hashtable的第一个数据取出来,要如何做才可以取出来呢?
ht.Add("First", "Hello");
ht.Add("Second", "World");
ht.Add("Third", "!");
foreach (object o in ht.Values)
Console.WriteLine(o);
请注意:IEnumerable接口
能够循环遍历
方法如下:
Hashtable table = new Hashtable();
table.Add("item1","张三");
table.Add("item2","李四");
IDictionaryEnumerator Enumerator=table.GetEnumerator();
int i=0;
while(Enumerator.MoveNext())
{
Console.WriteLine("Item: {0} Key:{1}",i,Enumerator.Key);
Console.WriteLine("Item: {0} Value:{1}",i,Enumerator.Value);
i++;
}
foreach(object o in table.Values)
{
Console.Write(o.ToString());
}
Console.Read();
但只是循环遍历而已,不能按照位置次序!正如sp1234所说!
如果你即要实现Hashtable又要实现ArrayList请使用System.Collections.Specialiazed.NameValueCollection!
ht.Add("First", "Hello");
ht.Add("Second", "World");
ht.Add("Third", "!");
ArrayList al = new ArrayList();
foreach (object o in ht.Values)
{
al.Add(o.ToString);
}
现在我想把数组里的第一个元素放到一个Hashtable类型的变量中,
我用这个方法就会出错;
Hashtable hst = new Hashtable();
hst = (Hashtable)al[0];
想用这样的方法来获取Hashtable里的值,并传给一个Hashtable类型的变量,但是这样会出错!