gpt4 book ai didi

c# - 如何将 ConcurrentDictionary 转换为字典?

转载 作者:IT王子 更新时间:2023-10-29 04:26:00 25 4
gpt4 key购买 nike

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

不允许在它们之间进行转换。那我该怎么做呢?

最佳答案

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

关于c# - 如何将 ConcurrentDictionary 转换为字典?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4330702/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com