What does this do exactly?
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;
Thanks
It populates a dictionary with 10 elements:
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
The first line will create a new Dictionary with strings as keys and int as values for these keys,
it is like a Hash Table.
The second line will execute the third line 10 times, with i from 0 to 9.
In the third line, we will store the string in the Dictionary together with its value in a way that it can be quicly looked up, with strings containing i + 1 which is 1 to 10 and set their corresponding value to 0.
STA Thread Queue Worker Thread No. 1 --> 0
STA Thread Queue Worker Thread No. 2 --> 0
...
Tip: It would be better to use an array for this, there is no need for hashing strings as they are static except for the integer. The string can thus be concatenated later when needed.
var counts = new int[10];
for (int i = 0; i < 10; i++)
counts[i] = 0;
Or even shorter, as the elements are 0 by default:
var counts = new int[10];
It creates a dictionary, with 10 entries in it:
Entry : 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
more specifically:
var counts = new Dictionary<string, int>();
create a dictionary in the field named counts. var means that the type is inferred.
for (int i = 0; i < 10; i++)
loop from 0 to 9 (inclusive)
counts[string.Format("STA Thread Queue Worker Thread No. {0}", i + 1)] = 0;
assign 0 the field with the key generated by that line of code.
string.Format("STA Thread Queue Worker Thread No. {0}", i + 1)
generate a string, assign the second argument passed into the Format method into the position of the string marked with {0}. eg
string.format("{0} {1} {2}", "foo", "bar" "bash")
Creates:
"foo bar bash"
it creates a dictionary (a lookup-table with string keys and integer values) and initializes it so that it holds the following data:
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
so for each of 10 worker threads (which aren't created or manipulated in this code) you get a value of 0 stored in the dictionary.
This creates a Dictionary<string, int>
, and initializes it with 10 entries that are all set to 0, which are keyed as:
STA Thread Queue Worker Thread No. 1
STA Thread Queue Worker Thread No. 2
...
STA Thread Queue Worker Thread No. 10
Just guessing, but it looks like this part of the code it used to initialize a dictionary to be used by 10 worker threads for storing result of the computations.
Possibly somewhere below the code is missing to actually spawn ten different threads writing to the corresponding "STA Thread Queue Worker Thread No. {0}"
key. After threads are finished the values could be aggregated.
Well, quite (but not formally) thread-safe, but if I guessed correct simple var counts = new int[10]
would do better.
精彩评论