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. net core体系-web应用程序-4asp.net core2.0 项目实战(1)-7项目缓冲方案( Redis)

    本文目录1. 摘要2. Redis配置3. RedisHelper4.使用实例 5. 总结 1.  摘要 由于內存存取速度远高于磁盘读取的特性,为了程序效率提高性能,通常会把常用的不常变动的数据存储在 ...

  2. Codeforces 1139E Maximize Mex 二分图匹配

    Maximize Mex 离线之后把删数变成加数, 然后一边跑匈牙利一遍算答案. #include<bits/stdc++.h> #define LL long long #define ...

  3. python3实现链表

    1.链表的实现 a.链表的结构为: b.链表的实现方法; #链表结构实现 私有属性_pro_item是指向下个节点的指针,_item为此节点的值 class ChainDemo(): def __in ...

  4. POJ 2352 Stars【树状数组】

    <题目链接> 题目大意: 题目给出n个点,这些点按照y坐标的升序,若y相同,则按照x的升序顺序输入,问,在这些点中,左下角的点的数量分别在0~n-1的点分别有多少个,写出它们的对应点数. ...

  5. Linux学习之源码包安装与脚本安装(十八)

    Linux学习之源码包安装与脚本安装 目录 源码包与RPM包的区别 源码包安装 脚本安装 源码包与RPM包的区别 1.区别 安装之前的区别:概念上的区别 安装之后的区别:安装位置不同 源码包: 开源的 ...

  6. python查看对象用法

    python查看类用法: dir(object_name)

  7. 【Spring Boot】构造、访问Restful Webservice与定时任务

    Spring Boot Guides Examples(1~3) 参考网址:https://spring.io/guides 创建一个RESTful Web Service 使用Eclipse 创建一 ...

  8. 浅谈云计算SPI(SaaS、PaaS、IaaS)

    The other day, I arrived at the SAP LABS CHINA for interview with my pleasure. That gave me a chance ...

  9. Django之路由

    Django的路由系统 URL配置(URLconf)就像Django所支撑网站的目录.它的本质是URL与要为该URL调用的视图函数之间的映射表. 我们就是以这种方式告诉Django,遇到哪个URL的时 ...

  10. Java实现FTP与SFTP文件上传下载

    添加依赖Jsch-0.1.54.jar <!-- https://mvnrepository.com/artifact/com.jcraft/jsch --> <dependency ...