本文介绍了我怎么能转换ConcurrentDictionary到字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

的<$c$c>ConcurrentDictionary<K,V>类实现 的IDictionary&LT; K,V&GT; 接口,这应该足以满足大多数要求。但是,如果你真的需要一个具体的<$c$c>Dictionary<K,V>...

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

// or...
// substitute your actual key and value types in place of TKey and TValue
var newDictionary = new Dictionary<TKey, TValue>(yourConcurrentDictionary);

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

09-25 05:01