Pregunta

Necesito depurar algún código antiguo que usa Hashtable para almacenar la respuesta de varios subprocesos.

Necesito una forma de revisar todo Hashtable e imprimir tanto las claves como los datos en Hastable.

¿Cómo se puede hacer esto?

¿Fue útil?

Solución

foreach(string key in hashTable.Keys)
{
   Console.WriteLine(String.Format("{0}: {1}", key, hashTable[key]));
}

Otros consejos

Me gusta:

foreach(DictionaryEntry entry in hashtable)
{
    Console.WriteLine(entry.Key + ":" + entry.Value);
}

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

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

Esto debería funcionar para prácticamente todas las versiones del framework...

foreach (string HashKey in TargetHash.Keys)
{
   Console.WriteLine("Key: " + HashKey + " Value: " + TargetHash[HashKey]);
}

El truco es que puedes obtener una lista/colección de las claves (o los valores) de un hash determinado para iterar.

EDITAR:Vaya, intentas mejorar un poco tu código y lo siguiente que sabes son 5 respuestas...8^D

También descubrí que esto también funcionará.

System.Collections.IDictionaryEnumerator enumerator = hashTable.GetEnumerator();

while (enumerator.MoveNext())
{
    string key = enumerator.Key.ToString();
    string value = enumerator.Value.ToString();

    Console.WriteLine(("Key = '{0}'; Value = '{0}'", key, value);
}

Gracias por la ayuda.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top