A coworker used a for loop to iterate a List in some C# code he wrote and left the comment, "did't use For Each because I wasn't sure it iterates in order. Who knows what Microsoft will do." For example, suppose we have a List built up like this:
var someList = new List<string>();
someList.Add("one");
someList.Add("two");
someList.Add("three");
my coworker used something like this:
for (int i = 0; i < someList.Count; i++)
{
System.Diagnostics.Debug.WriteLine(someList[i]);
}
instead of this:
foreach (var item in someList)
{
System.Diagnostics.Debug.WriteLine(item);
}
I guess he's afraid the items might come out in a different order than they were added to the collection. I think he's being a bit paranoid, but technically, the documentation does not state the order in which the collection is iterated. Is it possible for a foreach statement to traverse an array or collection object in any order other than from lowest bound to highest?