0

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}}
JoeB
  • 297
  • 4
  • 20
  • No, a collection class must have an Add() method to allow an initializer. ConcurrentDictionary has TryAdd(). Wanting to do this is quite strange and likely to be wrong. – Hans Passant Feb 01 '14 at 08:33

2 Answers2

1

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.

tinstaafl
  • 6,908
  • 2
  • 15
  • 22
0

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)
Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151