Question

Is it possible to declare and initialize a ConcurrentDictionary? Maybe something like the Dictionary:

Dim Stuff = New ConcurrentDictionary(Of Integer, Integer) From {{0, 1}, {2, 3}}
Was it helpful?

Solution

It looks like what you want is something like this:

Dim Stuff As New ConcurrentDictionary(Of Integer, Integer) _
    ({
            New KeyValuePair(Of Integer, Integer)(1, 2),
            New KeyValuePair(Of Integer, Integer)(3, 4),
            New KeyValuePair(Of Integer, Integer)(5, 6)})

ConcurrentDictionary can't use an initializer like the Dictionary since that method relies on having an Add method and ConcurrentDictionary only has AddOrUpdate.

OTHER TIPS

Just use a Dictionary as an intermediate storage, and this constructor of ConcurrentDictionary:

Dim Stuff = New Dictionary(Of Integer, Integer) From {{0, 1}, {2, 3}}
Dim concurrentStuff = New ConcurrentDictionary(Of Integer, Integer)(Stuff)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top