tags:

views:

45

answers:

3

how to create , store and retrive the values from hash table in c#?

A: 
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];
codekaizen
+1  A: 

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();
}
rahul
+1  A: 

Have a look at

'

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);
}
astander