如何在c#?
中创建,存储和检索哈希表中的值答案 0 :(得分:1)
// Initializes a new hashtable
Hashtable hTable = new Hashtable();
// Adds an item to the hashtable
hTable.Add("Name", "Jon");
// Loop through all the values in the hashtable
IDictionaryEnumerator enumHtable = hTable.GetEnumerator();
while (enumHtable.MoveNext())
{
string str = enumHtable.Value.ToString();
}
答案 1 :(得分:1)
看看
Hashtable hashtable = new Hashtable();
hashtable[1] = "One";
hashtable[2] = "Two";
hashtable[13] = "Thirteen";
foreach (DictionaryEntry entry in hashtable)
{
Console.WriteLine("{0}, {1}", entry.Key, entry.Value);
}
答案 2 :(得分:0)
Hashtable h = new Hashtable();
Object k = new Object(); // can be any type of object
Object v = new Object(); // can be any type of object
h[k] = v;
如果您使用的是.Net 2.0或更高版本,使用Dictionary<K, V>
可以更加有用,您可以在其中键入键和值。否则,您需要在获取值时强制转换值:
Hashtable h = new Hashtable();
Object k = new Object(); // can be any type of object
String v = "My value";
h[k] = v;
String value = (String)h[k];