would remove a key from Dictionary in foreach cause a problem? or should I better to construct a new Dictionary?
The first approach will crash at runtime, since the enumerator makes sure that nobody deletes from the underlying collection while it's enumerating.
The second approach is a nice thought, but C# dictionaries are mutable and it's neither idiomatic nor efficient to copy them around if you can accomplish the same thing with mutation.
This is a typical way:
var itemsToRemove = myDic.Where(f => f.Value == 42).ToArray();
foreach (var item in itemsToRemove)
myDic.Remove(item.Key);
EDIT: In response to your question in the comments. Here's how the example in your other question works:
myList = myList.where(x=>x>10).select(x=>x-10);
This line of code doesn't run anything; it's totally lazy. Let's say for the sake of argument that we have a foreach
after it to make it look more like this question's example.
foreach (int n in myList)
Console.WriteLine(n);
When that executes, here's what'll happen on each iteration:
- Call
MoveNext
on the enumerator - The enumerator finds the next value greater than ten
- Then it takes that value minus ten and sets the
Current
property to that - Binds the
Current
property to the variablen
Console.WriteLine
s it
You can see that there's no mystery and no infinite loop and no whatever.
Now compare to my example, supposing we left out the ToArray
.
var itemsToRemove = myDic.Where(f => f.Value == 42);
foreach (var item in itemsToRemove)
myDic.Remove(item.Key);
- Call
MoveNext
on the enumerator - The enumerator finds the next pair with value 42 and sets the
Current
property to that - Binds the
Current
property to the variableitem
Remove
s it
This doesn't work because while it's perfectly fine to WriteLine
something from a collection while you have an enumerator open on it, you aren't permitted to Remove
something from a collection while you have an enumerator open on it.
If you call ToArray
up front, then you start out by enumerating over the dictionary and populating the array. When we get to the foreach
, the foreach
statement has an enumerator open on the array, not the dictionary. You're allowed to remove from the dictionary as you iterate over the array.
Also you can iterate over the copy of your collection:
foreach (var item in myDic.ToList())
{
if (item.value == 42)
myDic.remove(item.key);
}
notice myDic.ToList()
in foreach
statement.