Home > DeveloperSection > Forums > Hashtable to Dictionary
Ankita Pandey
Ankita Pandey

Total Post:183

Posted on    December-17-2013 1:10 AM

 C# C# 

 1 Reply(s)
 1006  View(s)
Rate this:

I am trying to convert a hashtable to disctionary and found a a question here: 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);


When I try to use it, there is an error in table.Cast; intellisense does not show "Cast" as a valid method.

Pravesh Singh

Total Post:411

Posted on    December-17-2013 1:40 AM

Hi Ankita,

Enumerable.Cast doesn't exist in .NET 2, nor does most of the LINQ related methods (such as ToDictionary).

You'll need to do this manually via looping:

public static Dictionary<K,V> HashtableToDictionary<K,V> (Hashtable table)


    Dictionary<K,V> dict = new Dictionary<K,V>();

    foreach(DictionaryEntry kvp in table)

        dict.Add((K)kvp.Key, (V)kvp.Value);

    return dict;


Don't want to miss updates? Please click the below button!

Follow MindStick