Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print out the keys and Data of a Hashtable in C# .NET 1.1

I need debug some old code that uses a Hashtable to store response from various threads.

I need a way to go through the entire Hashtable and print out both keys and the data in the Hastable.

How can this be done?

like image 346
David Basarab Avatar asked Aug 29 '08 18:08

David Basarab


People also ask

Is there a hash table library in C?

The standard C library doesn't include any large, persistent data structures - neither lists, nor trees, nor stacks, nor hashtables.

What is key and value in Hashtable?

Hashtable stores key/value pair in hash table. In Hashtable we specify an object that is used as a key, and the value we want to associate to that key. The key is then hashed, and the resulting hash code is used as the index at which the value is stored within the table.

Why hashing is used in data structure?

Hashing provides constant time search, insert and delete operations on average. This is why hashing is one of the most used data structure, example problems are, distinct elements, counting frequencies of items, finding duplicates, etc.

What is the use of Hashtable in C#?

A hash table is used when you need to access elements by using key, and you can identify a useful key value. Each item in the hash table has a key/value pair. The key is used to access the items in the collection.


3 Answers

foreach(string key in hashTable.Keys)
{
   Console.WriteLine(String.Format("{0}: {1}", key, hashTable[key]));
}
like image 159
Ben Scheirman Avatar answered Sep 18 '22 12:09

Ben Scheirman


I like:

foreach(DictionaryEntry entry in hashtable)
{
    Console.WriteLine(entry.Key + ":" + entry.Value);
}
like image 20
Jake Pearson Avatar answered Sep 19 '22 12:09

Jake Pearson



   public static void PrintKeysAndValues( Hashtable myList )  {
      IDictionaryEnumerator myEnumerator = myList.GetEnumerator();
      Console.WriteLine( "\t-KEY-\t-VALUE-" );
      while ( myEnumerator.MoveNext() )
         Console.WriteLine("\t{0}:\t{1}", myEnumerator.Key, myEnumerator.Value);
      Console.WriteLine();
   }

from: http://msdn.microsoft.com/en-us/library/system.collections.hashtable(VS.71).aspx

like image 33
Dinah Avatar answered Sep 20 '22 12:09

Dinah