It is known that while iterating some IEnumerable in C#, no modifications can be made to elements of the enumerable collection:
// Illegal code
foreach (Employee e in employeeList)
{
e.Salary = 1000000;
}
I am wondering how is this being enforced by the runtime or by the enumerator itself?
I think you’re misunderstanding the restriction. The code you posted is perfectly legal, and in fact, if it were not then the
foreachloop would be pretty useless.What is not permitted is changing the sequence while in the middle of a
foreachloop:This code throw an
InvalidOperationExceptionat run-time: the call toMoveNextin theIEnumeratorimplementation will throw when it detects that the underlying collection has changed between calls. This is a requirement of implementing the enumerator object — each implementation ofIEnumerablehas 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 ofemployeeListwas, for example, aConcurrentDictionary. See this blog post for details.It is also not legal to assign a value to the loop control variable, e.g.:
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.