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?
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?
I think i have found a way to do it.
Why do you need to convert it to a Dictionary?
ConcurrentDictionary<K, V>
implements theIDictionary<K, V>
interface, is that not enough?If you really need a
Dictionary<K, V>
, you can copy it using LINQ:Note that this makes a copy. You cannot just assign a ConcurrentDictionary to a Dictionary, since ConcurrentDictionary is not a subtype of Dictionary. That's the whole point of interfaces like IDictionary: You can abstract away the desired interface ("some kind of dictionary") from the concrete implementation (concurrent/non-concurrent hashmap).
The
ConcurrentDictionary<K,V>
class implements theIDictionary<K,V>
interface, which should be enough for most requirements. But if you really need a concreteDictionary<K,V>
...