我怎么能转换ConcurrentDictionary到字典?字典、我怎么能、ConcurrentDictionary

2023-09-03 06:54:05 作者:我爱你来自口腔也经过心脏

我有一个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?

推荐答案

的ConcurrentDictionary<K,V>类实现 的IDictionary&LT; K,V&GT; 接口,这应该足以满足大多数要求。但是,如果你真的需要一个具体的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);
 
精彩推荐