如何将ConcurrentDictionary转换为字典? [英] How can I convert a ConcurrentDictionary to a Dictionary?

查看:195
本文介绍了如何将ConcurrentDictionary转换为字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ConcurrentDictionary对象,我想设置一个Dictionary对象。



不允许在它们之间投射。那么我该怎么办?

解决方案

ConcurrentDictionary< K,V> 类实现 IDictionary< K,V> 界面,应该足以满足大多数要求。但是,如果您真的需要具体的 词典< K,V> ; ...

  var newDictionary = yourConcurrentDictionary.ToDictionary(kvp => kvp.Key,
kvp => kvp.Value,
yourConcurrentDictionary.Comparer);

//或...
//替换您的实际键和值类型代替TKey和TValue
var newDictionary = new Dictionary< TKey,TValue>(yourConcurrentDictionary, yourConcurrentDictionary.Comparer);


I have a ConcurrentDictionary object that I would like to set to a Dictionary object.

Casting between them is not allowed. So how do I do it?

解决方案

The ConcurrentDictionary<K,V> class implements the IDictionary<K,V> interface, which should be enough for most requirements. But if you really need a concrete 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);

这篇关于如何将ConcurrentDictionary转换为字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆