I think you're misunderstanding the restriction. The code you posted is perfectly legal, and in fact, if it were not then the foreach
loop would be pretty useless.
What is not permitted is changing the sequence while in the middle of a foreach
loop:
foreach ( Employee e in employeeList )
{
if ( e.Salary > 10000000 )
{
employeeList.Remove(e);
}
}
This code throw an InvalidOperationException
at run-time: the call to MoveNext
in the IEnumerator
implementation will throw when it detects that the underlying collection has changed between calls. This is a requirement of implementing the enumerator object -- each implementation of IEnumerable
has to deal with this possibility for itself. Note that certain collections (the new concurrent ones from .NET 4.0) explicitly do not enforce this restrictions, and thus the code above would be legal of employeeList
was, for example, a ConcurrentDictionary
. See this blog post for details.
It is also not legal to assign a value to the loop control variable, e.g.:
foreach ( Employee e in employeeList )
{
if ( e.Salary > 10000000 )
{
e = new Employee { Salary = 999999 };
}
}
The reason this isn't allowed is because it doesn't make much sense and is almost certainly a bug; see this answer for details: Why is The Iteration Variable in a C# foreach statement read-only?
Note that this isn't attempting to change the elements in the sequence -- its merely attempting to change the value of the local variable used as the loop control variable. Also, this isn't enforced by the enumerator nor the run-time. It's a compile-time error, and is enforced by the C# compiler.