集合已修改,可能无法执行枚举操作
InvalidOperationException: Collection was modified; enumeration operation may not execute
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestForeache : MonoBehaviour
{
// Start is called before the first frame update
public List<int> lst = new List<int>();
void Start()
{
for (int i = 0; i < 10; i++)
{
lst.Add(i);
}
// 不报错
foreach (var item in lst)
{
if(item == 1)
{
lst.Remove(item);
break;
}
}
//报错
foreach (var item in lst)
{
if (item >6 )
{
lst.Remove(item);
}
}
}
}
https://www.cnblogs.com/wolf-sun/p/5721457.html