Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 4.0 在ConcurrentDictionary中插入值_C# 4.0_C#4 - Fatal编程技术网

C# 4.0 在ConcurrentDictionary中插入值

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>> { {

我正在尝试将值插入ConcurrentDictionary,我习惯于使用dictionary,因此这不起作用:

  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",
    };