38

How do I convert a HashTable to Dictionary in C#? Is it possible?

For example, if I have a collection of objects in a HashTable and I want to convert it to a dictionary of objects with a specific type, how can I do that?

Pang
  • 9,564
  • 146
  • 81
  • 122
RKP
  • 5,285
  • 22
  • 70
  • 111
  • Do you know the type of the `Dictionary` elements at compile-time or run-time? – Kirk Woll Jun 23 '11 at 14:36
  • Are all the objects (keys and values) of the HashTable castable to a specific target type that will be used as the generic parameter for the Dictionary? Or would you rather exclude those in the HashTable that are not of the appropriate type? – daveaglick Jun 23 '11 at 14:41
  • If possible you should put the object in a `Dictionary` to start with. The `HashTable` class is practically obsolete since `Dictionary` was introduced. As `Dictionary` is the generic replacement for `HashTable` your code would need minor adjustments to use a `Dictionary` instead. – Guffa Jun 23 '11 at 14:49
  • 1
    the type is known at compile time and all the objects in Hashtable are of the same type. I am working on a legacy app which uses hash tables – RKP Jun 23 '11 at 17:13

5 Answers5

73
public static Dictionary<K,V> HashtableToDictionary<K,V> (Hashtable table)
{
   return table
     .Cast<DictionaryEntry> ()
     .ToDictionary (kvp => (K)kvp.Key, kvp => (V)kvp.Value);
}
agent-j
  • 27,335
  • 5
  • 52
  • 79
  • thanks for the complete answer of converting to dictionary and also casting of the key and value to the given type. – RKP Jun 24 '11 at 10:04
10
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);
Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
  • 3
    thanks for the solution which doesn't require looping which is what I was looking for. however I accepted the other solution as answer because it does the casting to correct type as well and has an extension method defined for it. the above one returns generic object type for both key and value which gives no additional advantage over hashtable. – RKP Jun 24 '11 at 10:04
7

Extension method version of agent-j's answer:

using System.Collections;
using System.Collections.Generic;
using System.Linq;

public static class Extensions {

    public static Dictionary<K,V> ToDictionary<K,V> (this Hashtable table)
    {
       return table
         .Cast<DictionaryEntry> ()
         .ToDictionary (kvp => (K)kvp.Key, kvp => (V)kvp.Value);
    }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Roberto
  • 11,557
  • 16
  • 54
  • 68
4

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]);
}
Pang
  • 9,564
  • 146
  • 81
  • 122
alx
  • 257
  • 1
  • 2
0
    Hashtable openWith = new Hashtable();
    Dictionary<string, string> dictionary = new Dictionary<string, string>();

    // Add some elements to the hash table. There are no 
    // duplicate keys, but some of the values are duplicates.
    openWith.Add("txt", "notepad.exe");
    openWith.Add("bmp", "paint.exe");
    openWith.Add("dib", "paint.exe");
    openWith.Add("rtf", "wordpad.exe");

    foreach (string key in openWith.Keys)
    {
        dictionary.Add(key, openWith[key].ToString());
    }
Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179