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. Python_序列化和反序列化模块

    序列化:将对象转换为可通过网络传输或可存储到本地磁盘的数据格式的转换过程,称为序列化,反之,称为反序列化 json: 用来实现不同语言,不同程序直接的信息交互,json支持所有高级语言之间的序列化交互 ...

  2. 使用spark集成kudu做DDL

    spark对kudu表的创建 定义kudu的表需要分成5个步骤: 1:提供表名 2:提供schema 3:提供主键 4:定义重要选项:例如:定义分区的schema 5:调用create Table a ...

  3. 怎样把linux客户端用户禁止用 su命令来切换用户

    系统中有一个组叫做“wheel”,我们可以利用该组实现一些特殊的功能.我们可以将拥有su使用权限的用户加入到wheel组中并且对该组进行限制,那么只有在该组中的用户才有su的使用权限. 如要实现该功能 ...

  4. net core体系-web应用程序-4asp.net core2.0 项目实战(1)-5项目数据库操作封装操作-EF框架

    EF框架有三种基本的方式:DB First(数据库优先),Model First(模型优先),Code First(代码优先). Entity Framework4.1之前EF支持“Database  ...

  5. Java基础总结02:环境变量的配置

    (一)Windows系统下配置环境变量 ※在"系统变量"中设置3项属性JAVA_HOME.PATH.CLASSPATH(JDK1.5之后此项属性不必再配),若已存在则点击" ...

  6. ubuntu中git

    1.在ubuntu中安装git $ sudo apt-get install git git-core 2.配置本机的git $ git config --global user.name " ...

  7. 工作记录之 [ python请求url ] v s [ java请求url ]

    背景: 模拟浏览器访问web,发送https请求url,为了实验需求需要获取ipv4数据包 由于不做后续的内容整理(有内部平台分析),故只要写几行代码请求发送https请求url列表中的url即可 开 ...

  8. Java字符串处理

    代码: import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner s ...

  9. Windows下MySQL绿色版安装配置与使用

    Mysql-5.7.11-winx64操作步骤: 一.安装MySQL数据库 1.下载. 下载地址: http://downloads.mysql.com/archives/get/file/mysql ...

  10. SpringBoot使用JdbcTemplate

    前言 本文是对SpringBoot使用JdbcTemplate操作数据库的一个介绍,,提供一个小的Demo供大家参考. 操作数据库的方式有很多,本文介绍使用SpringBoot结合JdbcTempla ...