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. 如何将JSON字典转换为XML?
- 3. 如何将NSDictionary转换为Python字典?
- 4. 如何将元组转换为字典?
- 5. 如何将此unicode转换为字典?
- 6. 将字典输入转换为字典
- 7. 重构字典ConcurrentDictionary
- 8. 将字典转换为ArrayList
- 9. 将字典转换为JSOn
- 10. 将字典转换为XML
- 11. 将字典转换为HashEntry
- 12. 如何将csv转换为Python中字典的字典?
- 13. 如何将弦乐字典转换为Python字典?
- 14. 将NetworkX MultiDiGraph转换为字典或从字典中转换
- 15. 如何将字典的字典转换为列表到字典列表?
- 16. .NET - 字典锁定与ConcurrentDictionary
- 17. 将字符串转换为python字典
- 18. Python将字符串转换为字典
- 19. 将字典转换为字符串
- 20. 将python字典转换为字符串
- 21. 如何转换列表为字典
- 22. Enumerable如何转换为字典?
- 23. 如何将XML转换为词典
- 24. 如何将字符串转换为Python 3中的字典。
- 25. 如何将字典转换为一串关键字参数?
- 26. Python 2.6如何将此字符串转换为字典?
- 27. 在字典中,如何将字符串转换为列表?
- 28. 我如何将字符串转换为访问多维字典
- 29. 如何将两个字符串转换为单个字典?
- 30. 的Python 3 - 如何将字符串转换为字典
请注意,要复制的字典可能会使用非默认的“IEqualityComparer”,它不会以这种方式保留! 更好:`var newDict = dict.ToDictionary(kvp => kvp.Key,kvp => kvp.Value,dict.Comparer);` – 2014-12-01 16:12:37