I was just curious about this: the following code will not compile, because we cannot modify a foreach iteration variable:
foreach (var item in MyObj
You can't modify a collection while it's being enumerated. The second example only updates a property of the object, which is entirely different.
Use a for
loop if you need to add/remove/modify elements in a collection:
for (int i = 0; i < MyObjectList.Count; i++)
{
MyObjectList[i] = new MyObject();
}