Pergunta

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}}
Foi útil?

Solução

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.

Outras dicas

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)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top