What’s the best way to set all values in a C# Dictionary?
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.
That is a reasonable approach, although I would prefer:
The call to
ToList()makes this work, since it’s pulling out and (temporarily) saving the list of keys, so the iteration works.