开发者

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

开发者 https://www.devze.com 2022-12-12 15:51 出处:网络
If do: foreach(var a in col) { a.X = 1; } Will my iterat开发者_JS百科or over the collection become invalid?

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

精彩评论

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