简体   繁体   English

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

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

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 尝试了其他一些方法,但我有错误。

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()的调用使这项工作起作用,因为它正在拉出并(临时)保存键列表,因此迭代有效。

单线解决方案:

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

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

我不确定这是否是最好的方法,但我正在寻找一条线上的东西,这对我有用

mydict.Keys.ToList().ForEach(k => mydict[k] = false);

I profiled the difference between Billy's and Reed's solutions.我描述了 Billy 和 Reed 的解决方案之间的差异。 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.我在 VB 中重写了解决方案(因为我目前正在使用该语言进行编程)并使用 int 键(为简单起见),否则它是完全相同的代码。 I ran the code with a dictionary of 10 million entries with a value of "true" for each entry.我使用包含 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 实际上更快

Starting from .NET5 (see github ):从 .NET5 开始(参见github ):

foreach (var pair in dict) 
    dict[pair.Key] += 1;

For before .NET5:对于 .NET5 之前:

foreach (var key in dict.Keys.ToList())
    dict[key] += 1;

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

Do it the way you have it right now... foreach is slow.按照你现在的方式去做...... foreach 很慢。 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.codeproject.com/KB/cs/foreach.aspx
http://www.madprops.org/blog/for-vs-foreach-performance/ http://www.madprops.org/blog/for-vs-foreach-performance/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM