What's the best way to set all values in a C# Dictionary?
在c#字典中设置所有值的最好方法是什么?
Here is what I am doing now, but I'm sure there is a better/cleaner way to do this:
这是我现在正在做的,但我确信有更好的/更干净的方法可以做到:
Dictionary<string,bool> dict = GetDictionary();
var keys = dict.Keys.ToList();
for (int i = 0; i < keys.Count; i++)
{
dict[keys[i]] = false;
}
I have tried some other ways with foreach, but I had errors.
我尝试了一些其他的方法,但我有错误。
7 个解决方案
#1
63
That is a reasonable approach, although I would prefer:
这是一种合理的做法,尽管我宁愿:
foreach (var key in dict.Keys.ToList())
{
dict[key] = false;
}
The call to ToList()
makes this work, since it's pulling out and (temporarily) saving the list of keys, so the iteration works.
对ToList()的调用使这个工作得以完成,因为它正在拉出并(临时)保存键列表,因此迭代可以工作。
#2
21
A one-line solution:
一行的解决方案:
dict = dict.ToDictionary(p => p.Key, p => false);
#3
6
If you aren't using tri-state bools, then you can use HashSet<string>
, and call Clear()
to set the values to "false".
如果您没有使用tristate bools,那么您可以使用HashSet
#4
3
I'm not sure if it's the best way but I was looking something on a single line and this worked for me
我不确定这是不是最好的方法,但我在一条直线上找了一些东西,这对我很有效
mydict.Keys.ToList().ForEach(k => mydict[k] = false);
#5
2
I profiled the difference between Billy's and Reed's solutions. Polaris878, take good note of the results and remember that premature optimization is the root of all evil ;-)
我分析了比利和里德的解决方案之间的区别。Polaris878,记下结果并记住,过早优化是万恶之源;
I rewrote the solutions in VB (because I'm currently programming in that language) and used int keys (for simplicity), otherwise it's the exact same code. I ran the code with a dictionary of 10 million entries with a value of "true" for each entry.
我用VB重写了解决方案(因为我目前正在用这种语言编程),并使用int键(为了简单),否则就是完全相同的代码。我使用一个包含1000万个条目的字典运行代码,每个条目的值为“true”。
Billy Witch Doctor's original solution:
比利巫医的原始解决方案:
Dim keys = dict.Keys.ToList
For i = 0 To keys.Count - 1
dict(keys(i)) = False
Next
Elapsed milliseconds: 415
经过的毫秒:415
Reed Copsey's solution:
里德Copsey的解决办法:
For Each key In dict.Keys.ToList
dict(key) = False
Next
Elapsed milliseconds: 395
经过的毫秒:395
So in that case the foreach is actually faster.
在这种情况下,foreach实际上更快。
#6
-2
You could just pull out the ToList() and iterate directly over the dictionary items
您可以提取ToList()并直接遍历字典项
Dictionary<string, bool> dict = GetDictionary();
foreach (var pair in dict)
{
dict[pair.Key] = false;
}
#7
-4
Do it the way you have it right now... foreach is slow. foreach may be cleaner, but you take too much of a performance hit using it.
按照你现在的方式去做……foreach是缓慢的。因为每一个都可能更干净,但是你使用它会造成性能损失。
Edit:
http://www.codeproject.com/KB/cs/foreach.aspx
http://www.madprops.org/blog/for-vs-foreach-performance/
编辑:http://www.codeproject.com/KB/cs/foreach.aspx http://www.madprops.org/blog/for-vs-foreach-performance/
#1
63
That is a reasonable approach, although I would prefer:
这是一种合理的做法,尽管我宁愿:
foreach (var key in dict.Keys.ToList())
{
dict[key] = false;
}
The call to ToList()
makes this work, since it's pulling out and (temporarily) saving the list of keys, so the iteration works.
对ToList()的调用使这个工作得以完成,因为它正在拉出并(临时)保存键列表,因此迭代可以工作。
#2
21
A one-line solution:
一行的解决方案:
dict = dict.ToDictionary(p => p.Key, p => false);
#3
6
If you aren't using tri-state bools, then you can use HashSet<string>
, and call Clear()
to set the values to "false".
如果您没有使用tristate bools,那么您可以使用HashSet
#4
3
I'm not sure if it's the best way but I was looking something on a single line and this worked for me
我不确定这是不是最好的方法,但我在一条直线上找了一些东西,这对我很有效
mydict.Keys.ToList().ForEach(k => mydict[k] = false);
#5
2
I profiled the difference between Billy's and Reed's solutions. Polaris878, take good note of the results and remember that premature optimization is the root of all evil ;-)
我分析了比利和里德的解决方案之间的区别。Polaris878,记下结果并记住,过早优化是万恶之源;
I rewrote the solutions in VB (because I'm currently programming in that language) and used int keys (for simplicity), otherwise it's the exact same code. I ran the code with a dictionary of 10 million entries with a value of "true" for each entry.
我用VB重写了解决方案(因为我目前正在用这种语言编程),并使用int键(为了简单),否则就是完全相同的代码。我使用一个包含1000万个条目的字典运行代码,每个条目的值为“true”。
Billy Witch Doctor's original solution:
比利巫医的原始解决方案:
Dim keys = dict.Keys.ToList
For i = 0 To keys.Count - 1
dict(keys(i)) = False
Next
Elapsed milliseconds: 415
经过的毫秒:415
Reed Copsey's solution:
里德Copsey的解决办法:
For Each key In dict.Keys.ToList
dict(key) = False
Next
Elapsed milliseconds: 395
经过的毫秒:395
So in that case the foreach is actually faster.
在这种情况下,foreach实际上更快。
#6
-2
You could just pull out the ToList() and iterate directly over the dictionary items
您可以提取ToList()并直接遍历字典项
Dictionary<string, bool> dict = GetDictionary();
foreach (var pair in dict)
{
dict[pair.Key] = false;
}
#7
-4
Do it the way you have it right now... foreach is slow. foreach may be cleaner, but you take too much of a performance hit using it.
按照你现在的方式去做……foreach是缓慢的。因为每一个都可能更干净,但是你使用它会造成性能损失。
Edit:
http://www.codeproject.com/KB/cs/foreach.aspx
http://www.madprops.org/blog/for-vs-foreach-performance/
编辑:http://www.codeproject.com/KB/cs/foreach.aspx http://www.madprops.org/blog/for-vs-foreach-performance/