集合已修改枚举操作可能无法执行 [英] collection was modified enumeration operation may not execute

查看:112
本文介绍了集合已修改枚举操作可能无法执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public void DeleteAllListItem(ListBox LstDelete, string path)
      {
          ftp = new FTPOperations();
          foreach (ListItem i in LstDelete.Items)
          {
              ftp.DeleteFTP(path + i.Text);
              LstDelete.Items.Remove(i.Text);

          }
      }


错误:集合被修改,枚举操作可能无法执行


Error:collection was modified enumeration operation may not execute

推荐答案

错误消息非常明确,而且很明确:您已更改了foreach循环为使用,结果无法确定它在做什么.枚举集合时,您不能添加,删除或以其他方式修改集合.
正常的方法是:
1)建立要删除的项目的列表,然后在循环的最后将其删除.

2)改为使用for循环.
The error message is pretty explicit, and pretty clear: you have changed the collection that the foreach loop is using, and as a result it cannot be sure what it is doing. You cannot add to, delete from, or otherwise modify a collection while it is being enumerated.
The normal ways to do it are:
1) Build a list of items to remove in a separate list, then delete them at the end of the loop.
or
2) use a for loop instead.


这篇关于集合已修改枚举操作可能无法执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆