System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
来源:互联网 发布:unity3d simplemove 编辑:程序博客网 时间:2024/05/18 11:31
Author:刘宁
Augmentum.ld ShangHai
今天在查看系统的Log的时候发现下面的异常抛出:
System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
根据CallStack跟踪到product code中有一段相关代码(因为牵涉公司机密,现提供伪代码):
//Pseudocode
List<string> items = new List<string>();
items.Add("a");
items.Add("b");
foreach (string item in items)
{
if (item == "a")
{
items.Remove(item);
}
}
查了下google:You cannot change a collection while you are looping through it.
于是自己修改了下code如下:
//Pseudocode
List<string> items = new List<string>();
List<string> tempItems = new List<string>();
items.Add("a");
items.Add("b");
foreach (string item in items)
{
if (item == "a")
{
tempItems.Add(item);
}
}
foreach (string tempItem in tempItems)
{
items.Remove(tempItem);
}
再次编译执行,异常消失。
- System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
- InvalidOperationException: Collection was modified; enumeration operation may not execute.
- Error: Collection was modified; enumeration operation may not execute.
- metro 异常 Collection was modified; enumeration operation may not execute
- Unity报错:Collection was modified; enumeration operation may not execute
- System.InvalidOperationException: Operation is not valid due to the current state of the object.
- operation system not found
- System.InvalidOperationException
- System.InvalidOperationException
- System.InvalidOperationException: The stream does not support reading
- InvalidOperationException: Operation is not valid due to the current state of the object
- TempData may not works under distributed system
- collection was not processed by flush()
- System.InvalidOperationException: 实例失败。
- EF 之 System.InvalidOperationException
- “System.InvalidOperationException”类型异常
- System.InvalidOperationException: 缺少参数
- 解决 cmake_symlink_library: System Error: Operation not supported
- System.getProperties()(
- 2007年10月29日(27日,28日没写)
- ajaxfileupload&cropper实现图片上传与在线编辑
- 3G使用技巧及应该注意的问题
- 平台调用链接
- System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
- 百度地域性越来越强
- geekOs操作系统实验
- 头大中
- OracleDBConsoleorcl不能启动,报错误码2
- 第一篇文章
- TUN/TAP
- C/C++语言经典、实用、趣味程序设计编程百例精解 (10)
- 父页面里两个iframe之间互相访问