开发者

Does changing a item field in a Collection while iterating invalidates the collection?

If do:

foreach(var a in col) {
     a.X = 1;
}

Will my iterat开发者_JS百科or over the collection become invalid?

Thanks


No. You can access the members of the items in the collection. Your code is valid.

What you can't do is modifying the collection itself (by removing or adding items to it) while iterating.


It shouldn't cause a problem. Only if you try to modify the contents of col by doing col.Remove or col.Add would I imagine there would be a problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜