I'm trying to insert values into ConcurrentDictionary, I'm used to dictionary, so this is does not work:
public ConcurrentDictionary<string, Tuple<double, bool,double>> KeyWords = new
ConcurrentDictionary<string, Tuple<double, bool,double>>
{
{"Lake", 0.5, false, 1}
};
What is the correct way, hence I'm doing this into class.
Collection initializers are syntactic sugar for calls to a public Add()
method ... which ConcurrentDictionary
doesn't provide - it has an AddOrUpdate()
method instead.
An alternative you can use is an intermediate Dictionary<>
passed to the constructor overload that accepts an IEnumerable<KeyValuePair<K,V>>
:
public ConcurrentDictionary<string, Tuple<double, bool,double>> KeyWords =
new ConcurrentDictionary<string, Tuple<double, bool,double>>(
new Dictionary<string,Tuple<double,bool,double>>
{
{"Lake", Tuple.Create(0.5, false, 1.0)},
}
);
Note: I corrected your example to use Tuple.Create()
since tuples are not infered from initializers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With