ExtensionMethod.NET Home of 876 C#, Visual Basic, F# and Javascript extension methods


Upgrades a hashtable to a generic dictionary


/// <summary>
/// Converts a hashtable to a generic ditionary
/// </summary>
/// <typeparam name="TKey">The type of the key in the hashtable</typeparam>
/// <typeparam name="TValue">The type of the object in the hashtable</typeparam>
/// <param name="t">The hashtable</param>
/// <returns></returns>
public static Dictionary<TKey, TValue> Upgrade<TKey, TValue>(this Hashtable t)
    var dic = new Dictionary<TKey, TValue>();

    foreach (DictionaryEntry entry in t)
        dic.Add((TKey)entry.Key, (TValue)entry.Value);

    return dic;


Hashtable t1 = new Hashtable();
 t1.Add("pi", Math.PI);
 t1.Add("e", Math.E);

 var col = t1.Upgrade<string, double>();

Author: C.F.Meijers

Submitted on: 11 dec 2007

Language: C#

Type: System.Collections.Hashtable

Views: 4850