This is intentionally done to include the Copy List scenario.
For example, I do foreach( var a in list ) list.Remove(a) , I get an exception saying that the "collection was changed" when the listing was performed.
To fix this, I do: foreach( var a in list.ToList() ) list.Remove(a) .
If you need semantics along the lines of "convert to list, if it is not already one", you will have to process it yourself. In fact, you could write a neat extension method for this:
public static IList<T> ToListIfNotAlready( this IEnumerable<T> source ) { var list = source as IList<T>; return list == null ? source.ToList() : list; }
Yes, it could be the other way around, but LINQ designers chose this approach and had every right to do so, since no approach has a clear advantage in the general case.
Fyodor soikin
source share