关于foreach中对集合执行Add或者Remove操作引发枚举值被修改异常

方法传入集合List<string> ids;

执行操作后再次循环引发异常

foreach (string id in ids)
{
ids.Add("a");
}

更改为for循环可控制循环次数

int ic = ids.Count();
for (int j = 0; j < ic; j++)
{

//验证判断

//if(true)

//{

//ids.Remove(ids[j]);

//}

}

posted @ 2015-10-30 18:02  唐崇杨  阅读(928)  评论(2编辑  收藏  举报