Numeric index into HashTable

  • Thread starter Thread starter _eee_
  • Start date Start date
E

_eee_

I'm looking for a way to step through a hashtable while adding items
to the table (This precludes using enumerators. They don't like
HashTable alterations while they are active). There must be an
internal structure that can be accessed sequentially, like an array.
Is it accessible?

Or maybe there is another construct that will allow this. The main
reason I'm using a HashTable is to eliminate duplicate entries.
(This is why I had posted a query about DataTables for this)
 
Hi _eee_,

_eee_ said:
I'm looking for a way to step through a hashtable while adding items
to the table (This precludes using enumerators. They don't like
HashTable alterations while they are active). There must be an
internal structure that can be accessed sequentially, like an array.
Is it accessible?

Or maybe there is another construct that will allow this. The main
reason I'm using a HashTable is to eliminate duplicate entries.
(This is why I had posted a query about DataTables for this)

You could do it like this:

Hashtable hashTable = new Hashtable();
hashTable["foo"] = "bar";

object[] keys = new object[hashTable.Count];
hashTable.Keys.CopyTo(keys, 0);

for (int i = 0; i < keys.Length; i++)
{
Console.WriteLine("Key is {0}. Value is {1}",
keys.ToString(),
hashTable[keys].ToString());
}

Regards,
Dan
 
Back
Top