Quote:
Originally Posted by Zippygoose I am filing this under "great interview questions" btw. |
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.