for example:
1.
foreach (var item in myDic)
{
if (item.value == 42)
myDic.remove(item.key);
}
would the iterator works properly no matter how the statements in the inner brackets could possibly affect myDic?
2.
var newDic = myDic.where(x=>x.value!=42).ToDictionary(x=>x.key,x=>x.value);
Is 2nd approach a good practice? functional programming and immutable?
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:
EDIT: In response to your question in the comments. Here’s how the example in your other question works:
This line of code doesn’t run anything; it’s totally lazy. Let’s say for the sake of argument that we have a
foreachafter it to make it look more like this question’s example.When that executes, here’s what’ll happen on each iteration:
MoveNexton the enumeratorCurrentproperty to thatCurrentproperty to the variablenConsole.WriteLines itYou 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.MoveNexton the enumeratorCurrentproperty to thatCurrentproperty to the variableitemRemoves itThis doesn’t work because while it’s perfectly fine to
WriteLinesomething from a collection while you have an enumerator open on it, you aren’t permitted toRemovesomething from a collection while you have an enumerator open on it.If you call
ToArrayup front, then you start out by enumerating over the dictionary and populating the array. When we get to theforeach, theforeachstatement has an enumerator open on the array, not the dictionary. You’re allowed to remove from the dictionary as you iterate over the array.