This was a bug I had posted at Microsoft Connect Website, Microsoft already fixed it in upcoming .NET Edition as mentioned here in the following link.
List.ForEach allows enumeration over modified version of List
This probably isnt directly related to answer but its pretty funny of what I have just found out.
ForEach, delete (works)
List<int> list = new List<int>(){ 1, 2, 3, 4, 5, 6};
list.ForEach(x => {
Console.WriteLine(x);
list.Remove(x);
});
foreach, delete (crashes)
// throws exception
foreach (var x in list)
{
Console.WriteLine(x);
list.Remove(x);
}
ForEach, insert (...)
// goes in infinite loop...
list.ForEach(x => {
list.Add(1);
});
foreach, insert (crashes)
// throws exception
foreach (var x in list)
{
Console.WriteLine(x);
list.Add(x);
}
So anyone who is talking here about mutability or different confusing layers etc, I think it is completely half implemented feature by Visual Team because enumeration will always cause problems if the collection is modified.
Despite of arguments, I still see a no reason that ForEach should allow modifications, it is purely used for enumeration and it makes no difference whether its foreach(var item in x) syntax or x.ForEach(x=>{}) syntax.
I disagree with Eric, I just see it simply that BCL team has implemented this feature on IEnumerable as well as list.ForEach is faulty.
"Why" is completely subjective,for example, Silverlight has added complex hashing algorithms and left MD5 behind where else everywhere else we use MD5 so widely. Its more of how much anything is in demand and who is the one who chooses whether to include it or not in framework.
There is no logical or philosophical reason at all for not having ForEach in IEnumerable. There are many such missing points which I think .NET will improve over time.