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. String类型的转型

    字符串类型的转型在java中常用的方法有标题中的三种. 简单介绍: 1.toString,需要保证调用这个方法的类.方法.变量不为null,否则会报空指针. 2.String.valueOf.这个方法 ...

  2. elementui分页点击详情返回分页样式

    updated(){ $(".el-pager").children("li").removeClass("active"); var li ...

  3. java实现点选汉字验证码(转)

    package com.rd.p2p.web; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Font; im ...

  4. bootstrapTable

    一个详细的教程 table参数 bootstrap table使用总结 BootstrapTable使用实例 事件event 事件函数的用法: 方法1 $('#table').bootstrapTab ...

  5. C语言中的模运算-hdu6124(打表,找规律)

    题目链接:https://vjudge.net/problem/HDU-6124 题目描述: 题目大意就是给你一个数,判断这个数 % 其它数后共有几种结果. 这题对我来说最大的难点是我不太知道每个数 ...

  6. boost 正则表达式 regex

    boost 正则表达式 regex   环境安装 如果在引用boost regex出现连接错误,但是引用其他的库却没有这个错误,这是因为对于boost来说,是免编译的,但是,正则这个库 是需要单独编译 ...

  7. prev_permutation(a+1,a+n+1)

    prev_permutation(a+1,a+n+1)产生上一个排列

  8. Linux学习笔记 11

    移除文件 #rm -i file 有信息确认的文件删除 #rm file1 file2 有信息确认的文件删除 #rm -rf file 强制删除file文件

  9. PHP.ini 文件大小限制

    post_max_size=80000000000M upload_max_filesize=2000000000M

  10. vuex那些事儿

    vuex适用于大型单页应用.每一个Vuex应用的核心就是store(仓库),store中储存大量状态,Vuex的状态存储是响应式的.vuex使用单一状态树,一个对象包含了全部的应用层级状态,每个应用仅 ...