我一直对这个感到困惑。考虑以下循环:
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.
- 上面的 [list] 是硬编码的。如果在循环进行迭代时从外部更改列表,会发生什么?
- 如果 [list] 是只读属性
int List{get{return(new int [] {1,2,3});}}
怎么办?这会扰乱循环。如果不是,它会在每次迭代中创建一个新实例吗?