C# 4.0 在ConcurrentDictionary中插入值
我正在尝试将值插入ConcurrentDictionary,我习惯于使用dictionary,因此这不起作用:C# 4.0 在ConcurrentDictionary中插入值,c#-4.0,c#4,C# 4.0,C#4,我正在尝试将值插入ConcurrentDictionary,我习惯于使用dictionary,因此这不起作用: public ConcurrentDictionary<string, Tuple<double, bool,double>> KeyWords = new ConcurrentDictionary<string, Tuple<double, bool,double>> { {
public ConcurrentDictionary<string, Tuple<double, bool,double>> KeyWords = new
ConcurrentDictionary<string, Tuple<double, bool,double>>
{
{"Lake", 0.5, false, 1}
};
公共ConcurrentDictionary关键字=新建
并发字典
{
{“湖”,0.5,假,1}
};
正确的方法是什么,因此我在类中这样做。是对公共Add()
方法调用的语法糖。。。ConcurrentDictionary没有提供什么-它有一个方法
您可以使用的另一种方法是将中间字典
传递给接受IEnumerable
的构造函数重载:
公共ConcurrentDictionary关键字=
新ConcurrentDictionary(
新词典
{
{“Lake”,Tuple.Create(0.5,false,1.0)},
}
);
注意:我更正了您的示例,使用了Tuple.Create()
,因为元组不是从初始值设定项推断出来的。您可以使用:
var dict=新的ConcurrentDictionary
{
[0]=“零”,
[1] =“一个”,
};
是的,我知道问题是谢谢,我想我会在类构造函数中添加ConcurrentDictionary值。你同意这种方法吗?这也是一种合理的选择。
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)},
}
);
var dict = new ConcurrentDictionary<int, string>
{
[0] = "zero",
[1] = "one",
};