insert values in ConcurrentDictionary
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&g开发者_如何转开发t;> 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.
You can use dictionary initializer:
var dict = new ConcurrentDictionary<int, string>
{
[0] = "zero",
[1] = "one",
};
Yes I know the question is c#4
精彩评论