这究竟是做什么的?
var counts = new Dictionary<string, int>();
for (int i = 0; i < 10; i++)
counts[string.Format("STA Thread Queue Worker Thread No. {0}", i + 1)] = 0;
由于
答案 0 :(得分:11)
它填充了一个包含10个元素的字典:
Key | Value
----------------------------------------|------
"STA Thread Queue Worker Thread No. 1" | 0
"STA Thread Queue Worker Thread No. 2" | 0
"STA Thread Queue Worker Thread No. 3" | 0
"STA Thread Queue Worker Thread No. 4" | 0
"STA Thread Queue Worker Thread No. 5" | 0
"STA Thread Queue Worker Thread No. 6" | 0
"STA Thread Queue Worker Thread No. 7" | 0
"STA Thread Queue Worker Thread No. 8" | 0
"STA Thread Queue Worker Thread No. 9" | 0
"STA Thread Queue Worker Thread No. 10" | 0
答案 1 :(得分:2)
它创建一个字典,其中包含10个条目:
条目:值
"STA Thread Queue Worker Thread No. 1" : 0
"STA Thread Queue Worker Thread No. 2" : 0
"STA Thread Queue Worker Thread No. 3" : 0
"STA Thread Queue Worker Thread No. 4" : 0
"STA Thread Queue Worker Thread No. 5" : 0
"STA Thread Queue Worker Thread No. 6" : 0
"STA Thread Queue Worker Thread No. 7" : 0
"STA Thread Queue Worker Thread No. 8" : 0
"STA Thread Queue Worker Thread No. 9" : 0
"STA Thread Queue Worker Thread No. 10" : 0
更具体地说:
var counts = new Dictionary<string, int>();
在名为counts的字段中创建字典。 var表示推断出类型。
for (int i = 0; i < 10; i++)
循环从0到9(含)
counts[string.Format("STA Thread Queue Worker Thread No. {0}", i + 1)] = 0;
使用该行代码生成的密钥为该字段分配0。
string.Format("STA Thread Queue Worker Thread No. {0}", i + 1)
生成一个字符串,将传递给Format方法的第二个参数分配到标有{0}的字符串的位置。例如
string.format("{0} {1} {2}", "foo", "bar" "bash")
创建:
"foo bar bash"
答案 2 :(得分:2)
第一行将创建一个新的Dictionary,其中字符串为键,int为这些键的值,
它就像一个Hash Table。
第二行将执行第三行10次,i从0到9。
在第三行中,我们将字符串中的字符串与其值一起存储,以便可以快速查找,字符串包含i + 1,即1到10,并将其对应的值设置为0。
STA Thread Queue Worker Thread No. 1 --> 0
STA Thread Queue Worker Thread No. 2 --> 0
...
提示:最好为此使用一个数组,不需要散列字符串,因为它们是静态的,除了整数。因此,字符串可以在以后需要时连接。
var counts = new int[10];
for (int i = 0; i < 10; i++)
counts[i] = 0;
甚至更短,因为默认情况下元素为0:
var counts = new int[10];
答案 3 :(得分:1)
它创建一个字典(一个包含字符串键和整数值的查找表)并初始化它,以便它保存以下数据:
key => value
STA Thread Queue Worker Thread No. 1 => 0
STA Thread Queue Worker Thread No. 2 => 0
STA Thread Queue Worker Thread No. 3 => 0
STA Thread Queue Worker Thread No. 4 => 0
STA Thread Queue Worker Thread No. 5 => 0
...
STA Thread Queue Worker Thread No. 10 => 0
因此,对于每个10个工作线程(在此代码中未创建或操作),您将获得存储在字典中的值0。
答案 4 :(得分:0)
这将创建一个Dictionary<string, int>
,并将其初始化为10个条目,这些条目都设置为0,其键入为:
STA Thread Queue Worker Thread No. 1
STA Thread Queue Worker Thread No. 2
...
STA Thread Queue Worker Thread No. 10
答案 5 :(得分:0)
只是猜测,但它看起来像是用来初始化一个字典的部分代码,供10个工作线程用来存储计算结果。
可能缺少代码下方某处实际产生十个不同的线程写入相应的"STA Thread Queue Worker Thread No. {0}"
密钥。线程完成后,可以聚合值。
嗯,相当(但不是正式)线程安全,但如果我猜错了,简单的var counts = new int[10]
会更好。