質問

辞書オブジェクトに設定したいCONCURRENTDICTIONARYオブジェクトがあります。

それらの間のキャストは許可されていません。では、どうすればいいですか?

役に立ちましたか?

解決

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);

他のヒント

なぜそれを辞書に変換する必要があるのですか? ConcurrentDictionary<K, V> 実装します IDictionary<K, V> インターフェース、それだけでは不十分ですか?

本当に必要な場合 Dictionary<K, V>, 、 あなたはできる コピー linqを使用して:

var myDictionary = myConcurrentDictionary.ToDictionary(entry => entry.Key,
                                                       entry => entry.Value);

これがaをすることに注意してください コピー. 。君 できません concurrentdictionaryは辞書のサブタイプではないため、dictionaryに同時判決を割り当てるだけです。これが、Idictionaryのようなインターフェイスの全体的なポイントです。コンクリートの実装(同時/非currentハッシュマップ)から、目的のインターフェイス(「ある種の辞書」)を抽象化できます。

私はそれをする方法を見つけたと思います。

ConcurrentDictionary<int, int> concDict= new ConcurrentDictionary<int, int>( );
Dictionary dict= new Dictionary<int, int>( concDict);
ConcurrentDictionary<int, string> cd = new ConcurrentDictionary<int, string>();
Dictionary<int,string> d = cd.ToDictionary(pair => pair.Key, pair => pair.Value);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top