在 C# Dictionary<string,bool> 中设置所有值的最佳方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1167071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 09:50:10  来源:igfitidea点击:

What's the best way to set all values in a C# Dictionary<string,bool>?

c#.netgenericsdictionary

提问by Ronnie Overby

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.

我用 foreach 尝试了其他一些方法,但我有错误。

采纳答案by Reed Copsey

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()使这项工作,因为它拉出并(临时)保存键列表,所以迭代工作。

回答by AgileJon

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

回答by Polaris878

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 很慢。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/

回答by Sam Harwell

If you aren't using tri-state bools, then you can use HashSet<string>, and call Clear()to set the values to "false".

如果您不使用三态布尔值,则可以使用HashSet<string>, 并调用Clear()将值设置为“false”。

回答by Meta-Knight

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 ;-)

我描述了 Billy 和 Reed 的解决方案之间的差异。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:

里德科普西的解决方案:

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 实际上更快

回答by jeatsy

A one-line solution:

单线解决方案:

dict = dict.ToDictionary(p => p.Key, p => false);

回答by Daniele D.

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);