I've always been confused about this one. Consider the following loops:
int [] list = new int [] { 1, 2, 3 };
for (int i=0; i < list.Length; i++) { }
foreach (int i in list) { }
while (list.GetEnumerator().MoveNext()) { } // Yes, yes you wouldn't call GetEnumerator with the while. Actually never tried that.
- The [list] above is hard-coded. If the list was changed externally while the loop was going through iterations, what would happen?
- What if the [list] was a readonly property e.g.
int List{get{return(new int [] {1,2,3});}}
? Would this upset the loop. If not, would it create a new instance in each iteration?