Convert HashTable to Dictionary in C#
public static Dictionary<K,V> HashtableToDictionary<K,V> (Hashtable table)
{
return table
.Cast<DictionaryEntry> ()
.ToDictionary (kvp => (K)kvp.Key, kvp => (V)kvp.Value);
}
var table = new Hashtable();
table.Add(1, "a");
table.Add(2, "b");
table.Add(3, "c");
var dict = table.Cast<DictionaryEntry>().ToDictionary(d => d.Key, d => d.Value);
You can create an extension method for that
Dictionary<KeyType, ItemType> d = new Dictionary<KeyType, ItemType>();
foreach (var key in hashtable.Keys)
{
d.Add((KeyType)key, (ItemType)hashtable[key]);
}