Quote:
Originally Posted by Fog In my intro C# CS classes I found that one of the most pervasive mistakes was people who overused foreach, even people who were not new to programming. A lot of people would use a foreach loop to enumerate things like elements of an array, just because it was a handy shortcut to getting a reference to each item, in situations where they actually needed to get each element in order by index.
Often, it would be an invisible bug, because the code would work properly. Most of Microsoft's collections like that will return their elements in order if you enumerate them with a foreach. It's awful practice though because there's no guarantee that they will enumerate them the same way twice; in the next version of the .NET framework your favorite foreach might spit out items in a different order and break your code. Not good. |
That's people not understanding iterators though or the collection they are using. Never ever use a collection that requires a iterator, if order matters, if you want index or grab a object at a certain index when you can only get a iterator, you're using the wrong collection.