public void ForeachDic()
{
Dictionary dic = new Dictionary();
dic.Add("1", 10);
dic.Add("2", 20);
dic.Add("3", 30);
foreach (KeyValuePair kvp in dic)
{
Console.WriteLine(String.Format("Key:{0}; Value:{1}", kvp.Key, kvp.Value));
dic[kvp.Key] = 100;//此操作会报错:集合已修改;可能无法执行枚举操作。
}
}

解决方法就是我们可以另外创建一个数组来循环修改集合值,代码如下:

private void ForeachDic()
{
Dictionary dic = new Dictionary();
dic.Add("1", 10);
dic.Add("2", 20);
dic.Add("3", 30);
String[] keyArr = dic.Keys.ToArray();
for (int i = 0; i

Collection was modified; enumeration operation may not execute Dictionary 集合已修改;可能无法执行枚举操作的更多相关文章

  1. List使用Foreach 修改集合时,会报错的解决方案 (Error: Collection was modified; enumeration operation may not execute. ) - 摘自网络

    当用foreach遍历Collection时,如果对Collection有Add或者Remove操作时,会发生以下运行时错误: "Collection was modified; enume ...

  2. 解决Collection was modified; enumeration operation may not execute异常

    今天在使用foreach循环遍历list集合时,出现Collection was modified; enumeration operation may not execute.这个错误,查了半天才发 ...

  3. C# Collection was modified;enumeration operation may not execute

    一.问题描述 在做 数组.列表.集合遍历时,可能我们会遇见这个问题.Collection was modified;enumeration operation may not execute ,翻译的 ...

  4. Collection was modified; enumeration operation may not execute.的异常处理

    Collection was modified; enumeration operation may not execute.的异常处理 在运行程序时遇到这样一段异常,仔细检查后发现是使用Foreac ...

  5. Error: Collection was modified; enumeration operation may not execute.

    http://blog.csdn.net/ffeiffei/article/details/6131254

  6. [Robot Framework] Robot Framework用Execute Javascript对XPath表示的元素执行scrollIntoView操作

    有些元素需要通过滚动条滚动才能变得可见. 如果这些元素在DOM结构里面存在,可以通过scrollIntoView让其可见,但如果在DOM结构里面不存在那就要通过拖动滚动条让其变的可见. Execute ...

  7. [Robot Framework] Robot Framework用Execute Javascript对XPath表示的元素执行Click操作

    Execute Javascript document.evaluate("//a[contains(@href,'createBook')]", document, null, ...

  8. svn执行update操作后出现:Error : Previous operation has not finished; run 'cleanup' if it was interrupted.

    svn执行update操作后出现:      Error : Previous operation has not finished; run 'cleanup' if it was interrup ...

  9. 关于struts2的checkboxlist、select等标签发生could not be resolved as a collection/array/map/enumeration/iterator type异常的记录

    1 刚进入该界面的时候发生错误,原因是 list="roles"中的这个集合是空的,导致错误 解决办法很简单,不能让list为空 2 刚进入该界面的时候list是有数据的,当点击提 ...

随机推荐

  1. jQuery懒加载插件 – jquery.lazyload.js简单调用

    Lazy Load 是一个用 JavaScript 编写的 jQuery 插件. 它可以延迟加载长页面中的图片. 在浏览器可视区域外的图片不会被载入, 直到用户将页面滚动到它们所在的位置. 这与图片预 ...

  2. JavaScript onmousewheel鼠标滚轮示例

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  3. 2018牛客网暑假ACM多校训练赛(第四场)A Ternary String 数论

    原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round4-A.html 题目传送门 - https://www.no ...

  4. BZOJ4456/UOJ#184[Zjoi2016]旅行者 分治 最短路

    原文链接http://www.cnblogs.com/zhouzhendong/p/8682133.html 题目传送门 - BZOJ4456 题目传送门 - UOJ#184 题意 $n\times ...

  5. kmp基础 ekmp

    +]; int lenp,lens; +];//可以是char 也可以是string +]; void getnext() { nex[]=-; ,j=; ) { ||p[j]==p[k]) nex[ ...

  6. html 水平竖直居中

    line-height:容器高度 <!DOCTYPE html> <html lang="en"> <head> <meta charse ...

  7. 上传前端webuploader

    多文件上传时,是有几个文件调用几次方法. 可以设置为单线程.

  8. 20165220 Java第四周学习总结

    教材学习内容总结 super:使用关键字super来访问和调用被子类隐藏的成员变量和方法. 接口:用关键字interface来定义一个接口.接口由类来实现以便使用接口中的方法,用关键字implemen ...

  9. c++简单学习

    在c++中我们很容易遇到字符串的分割处理问题,这种问题通常比较容易,但由于我比较菜,花费了一定时间去思考一个和字符串相关的题,该题的大概思路是利用取模运算后,将得到的单个字符进行分析,主要考察到了字符 ...

  10. linux 学习笔记 软件包管理

    >查询文件所属软件包 #rpm -qf /usr/share/pixmaps/xplns.png 查询某个文件所属软件包 >查询软件包所包含文件列表 #rpm -ql xplus-3.3. ...