我想轉換一個哈希表來disctionary,發現了一個問題在這裏: 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);
}
當我嘗試使用它,就在table.Cast錯誤;智能感知不會將「Cast」顯示爲有效的方法。
C#2不支持LINQ,所以你不能做到這一點。 – SLaks
你有'使用System.Linq'嗎? – Sebastian
謝謝你塞巴斯蒂安;這是我的問題;我錯過了「使用..」這一行。我現在有的代碼行很好。我也試過「Dictionary dict = HashtableToDictionary (htOffice);」我的散列表使用一個字符串作爲鍵和一個int作爲值。不知道如何將你的回答標記爲答案。 –
NoBullMan