List使用Foreach 修改集合时,会报错的解决方案 (Error: Collection was modified; enumeration operation may not execute. ) - 摘自网络
当用foreach遍历Collection时,如果对Collection有Add或者Remove操作时,会发生以下运行时错误:
"Collection was modified; enumeration operation may not execute." 如下所示:
[c-sharp] view plaincopy
List<string> list = new List<string>();
for (int i = 0; i < 10; i++)
{
list.Add(i.ToString());
}
foreach (string str in list)
{
//will throw InvalidOperationException in runtime.
list.Remove(str);
}
究其原因,是因为Collection返回的IEnumerator把当前的属性暴露为只读属性,所以对其的修改会导致运行时错误,只需要把foreach改为for来遍历就好了。
对于Hashtable,由于没有提供[]操作符来供for遍历,可以先把item存入到一个array中,然后再对其进行Remove或Add操作,如下:
[c-sharp] view plaincopy
Hashtable ht = new Hashtable();
for(int i=0;i<10;i++)
{
ht.Add(i, i);
}
List<object> keys = new List<object>();
foreach (DictionaryEntry de in ht)
{
/*
operations for ht
* */
//store related keys
keys.Add(de);
}
//another for loop to operate ht
for (int i = 0; i < keys.Count; i++)
{
/*
operations for tagged keys
*/
}
List使用Foreach 修改集合时,会报错的解决方案 (Error: Collection was modified; enumeration operation may not execute. ) - 摘自网络的更多相关文章
- Collection was modified; enumeration operation may not execute Dictionary 集合已修改;可能无法执行枚举操作
public void ForeachDic() { Dictionary dic = new Dictionary(); dic.Add("1", 10); dic.Add(&q ...
- 替换openjdk的版本时遇到报错Transaction check error
x想要使用jmap对jvm内存进行排查问题,但是默认安装的openjdk包中并不带有这个命令,需要新升级到新版本才有 而在安装新的版本时,遇到报错: : file /usr/lib64/libns ...
- Foeach 时修改集合的值报错
就是"集合已修改:可能无法执行枚举操作 foreach" 啥的, 不让我改 百度到Foreach是只读的,只供取值用,无法进行新增,修改,删除(仅引用,实际待验证) 解决办法:将F ...
- foreach 改变集合时不能使用
使用foreach循环遍历list集合时,出现Collection was modified; enumeration operation may not execute.这个错误,查了半天才发现是当 ...
- 【EWM系列】SAP EWM模块-修改任何内容都报错
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[EWM系列]SAP EWM模块-修改任何内容都 ...
- PHP编译安装报错:configure: error: mcrypt.h not found. Please reinstall libmcrypt
我是在CentOS6.5安装php5.5.28这个版本,PHP编译代码如下: ./configure --prefix=/usr/local/php --with-config-file-path=/ ...
- 【PostgreSQL】PostgreSQL添加新服务器连接时,报错“Server doesn't listen ”,已解决。
PostgreSQL添加新的服务器连接时,报错:
- saltstack配置安装的一些关键步骤及安装时各种报错的分析
以下其他仅做参考,官方网址才是安装重点:http://docs.saltstack.cn/topics/installation/rhel.html 与安装相关的一些文档或资料: 一.linux服务器 ...
- 修改oracle数据库内存报错
今天修改oracle数据库内存时, alter system set memory_max_target=10240M scope=spfile;语句正确修改:但重启时却报错 : SQL> al ...
随机推荐
- Random.Next获取随即数
Random random = new Random(); random.Next()--------------返回非负的一个随机数. random.Next(int maxValue)----- ...
- 网站开启Gzip压缩-apache
找到并打开apache/conf目录中的httpd.conf文件 httpd.conf中打开deflate_Module和headers_Module模块,具体做法为将 如下两句前面的#去掉: Loa ...
- 简单的网页布局效果html5+CSS3
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Spring中argNames的含义
最近学习Spring,一直不太明白Srping的切面编程中的的argNames的含义,经过学习研究后,终于明白,分享一下 先看一个例子: 需要监控的类: package bean; public cl ...
- Burp Suite Walkthrough(中文版)
Burp Suite是Web应用程序测试的最佳工具之一,其多种功能可以帮我们执行各种任务.请求的拦截和修改,扫描web应用程序漏洞,以暴力破解登陆表单,执行会话令牌等多种的随机性检查.本文将做一个Bu ...
- hdu 3480
斜率dp #include<cstdio> #include<cstring> #include<algorithm> #include<queue> ...
- 常用的python模块
http://tiankonghaikuo1000.blog.163.com/blog/static/18231597200812424255338/ adodb:我们领导推荐的数据库连接组件bsdd ...
- *[topcoder]LittleElephantAndString
http://community.topcoder.com/stat?c=problem_statement&pm=12854&rd=15709 这道题DIV1 250的,还有点意思. ...
- win7桌面图标小盾牌怎么去掉(2种方法)
很多用户都会在桌面上放置一些常用的程序图标,由于win7系统提高了系统安全性,新增用户帐户控制,所以会在图标上显示小盾牌,表示需要管理员权限打开.不少win7 32位旗舰版用户觉得这个小盾牌很碍眼,那 ...
- ActionBar官方教程(5)ActionBar的分裂模式(底部tab样式),隐藏标题,隐藏图标
Using split action bar Split action bar provides a separate bar at the bottom of the screen to displ ...