2008-08-29 31 views

Respuesta

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

Me gustaría que hubiera un camino más corto para hacer esto con LINQ ... – Pellet

+1

@Pellet usted debe ser capaz de ... '(de llave de cadena en hashTable.Keys seleccione nuevo {key, hashTable [key]}) ' –

1

Esto debería funcionar para casi todas las versiones del marco ...

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

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

EDIT: Wow, intenta bastante el código de una cosa pequeña y al lado el ya sabe que hay 5 respuestas ... 8^D

1

También encontré que esto funcionará también.

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.

9

me gustan:

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

Console.WriteLine (entry.Key +": "+ entry.Value); // falta el corsé de cierre –

Cuestiones relacionadas