convert HashTable to Dictionary in C#

RKP picture RKP · Jun 23, 2011 · Viewed 26.5k times · Source

how to convert a HashTable to Dictionary in C#? is it possible? for example if I have collection of objects in HashTable and if I want to convert it to a dictionary of objects with a specific type, how to do that?

Answer

agent-j picture agent-j · Jun 23, 2011
public static Dictionary<K,V> HashtableToDictionary<K,V> (Hashtable table)
{
   return table
     .Cast<DictionaryEntry> ()
     .ToDictionary (kvp => (K)kvp.Key, kvp => (V)kvp.Value);
}