how to create , store and retrieve the values from hash table in c#?
How to create ,开发者_运维技巧 store and retrieve the values from hash table in c#?
Have a look at
- C# Hashtable Use, Lookups and Examples
- How to work with the HashTable collection in Visual C#
'
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);
}
Take a look at Hashtable Class
// 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();
}
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;
If you are working in .Net version 2.0 or above, it can be more useful to use a Dictionary<K, V>
where you can type the keys and values. Otherwise, you need to cast the values when you get them:
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];
精彩评论