19
A
回答
31
ConcurrentDictionary<K,V>
類實現IDictionary<K,V>
接口,該接口對於大多數需求應該足夠了。但是,如果你真的需要一個具體的Dictionary<K,V>
...
var newDictionary = yourConcurrentDictionary.ToDictionary(kvp => kvp.Key,
kvp => kvp.Value,
yourConcurrentDictionary.Comparer);
// or...
// substitute your actual key and value types in place of TKey and TValue
var newDictionary = new Dictionary<TKey, TValue>(yourConcurrentDictionary, yourConcurrentDictionary.Comparer);
9
爲什麼你需要將它轉換爲字典? ConcurrentDictionary<K, V>
實現了IDictionary<K, V>
接口,這是不夠的?
如果你真的需要一個Dictionary<K, V>
,你可以複製使用LINQ它:
var myDictionary = myConcurrentDictionary.ToDictionary(entry => entry.Key,
entry => entry.Value);
注意,這使得複製。你不能只是分配一個ConcurrentDictionary到一個字典,因爲ConcurrentDictionary不是一個字典的子類型。這就是IDictionary這樣的接口的全部要點:您可以從具體實現(併發/非併發哈希映射)中抽象出所需的接口(「某種字典」)。
0
ConcurrentDictionary<int, string> cd = new ConcurrentDictionary<int, string>();
Dictionary<int,string> d = cd.ToDictionary(pair => pair.Key, pair => pair.Value);
3
我想我已經找到了一種方法來做到這一點。
ConcurrentDictionary<int, int> concDict= new ConcurrentDictionary<int, int>();
Dictionary dict= new Dictionary<int, int>(concDict);
相關問題
- 1. 如何將字典轉換爲ConcurrentDictionary?
- 2. 如何將字典列表轉換爲Python字典字典
- 3. 如何將字典轉換爲數組
- 4. 如何將NSDictionary轉換爲Python字典?
- 5. 如何將此unicode轉換爲字典?
- 6. 如何將JSON字典轉換爲XML?
- 7. 如何將元組轉換爲字典?
- 8. 如何將列表轉換爲字典?
- 9. 將字典列表轉換爲字典
- 10. 將字典轉換爲字典
- 11. 將字典輸入轉換爲字典
- 12. 將字典轉換爲ArrayList
- 13. 將字典轉換爲HashEntry
- 14. 將類轉換爲字典
- 15. 將字典轉換爲XML
- 16. 將字典轉換爲OrderedDict
- 17. 將jSon轉換爲字典
- 18. 將字典轉換爲JSOn
- 19. 重構字典ConcurrentDictionary
- 20. 如何將字符串轉換爲字典轉爲蟒蛇
- 21. 如何將字典列表轉換爲字典?
- 22. 如何將元組字典轉換爲列表字典(Python)?
- 23. 如何將csv轉換爲Python中字典的字典?
- 24. 如何將絃樂字典轉換爲Python字典?
- 25. 將NetworkX MultiDiGraph轉換爲字典或從字典中轉換
- 26. 如何將字典的字典轉換爲列表到字典列表?
- 27. 如何將XML轉換爲詞典字典
- 28. 將字典轉換爲字符串
- 29. Python將字符串轉換爲字典
- 30. 將python字典轉換爲字符串
請注意,要複製的字典可能會使用非默認的「IEqualityComparer」,它不會以這種方式保留! 更好:`var newDict = dict.ToDictionary(kvp => kvp.Key,kvp => kvp.Value,dict.Comparer);` – 2014-12-01 16:12:37