日期:2014-05-17  浏览次数:20825 次

两个list,循环一个,另一个Remove报错,想不明白 求解
C# code

        List<Model.ElectricityMeterInfo> operateModels = new List<DaHuaElectricMeter.Model.ElectricityMeterInfo>();
        List<Model.ElectricityMeterInfo> operateTmpModels = new List<DaHuaElectricMeter.Model.ElectricityMeterInfo>();

 foreach (Model.ElectricityMeterInfo emi in Command.SystemData.emiModels)
                    {
                        operateModels.Add(emi);
                        operateTmpModels.Add(emi);
                    }
 foreach (Model.ElectricityMeterInfo e in operateModels) 
{
  operateTmpModels.Remove(e);
}


报错了,两个List的count全部都改变,我就想不明白是为什么

------解决方案--------------------
C# code
            
            List<int> list1 = new List<int>();
            List<int> list2 = new List<int>();

            for (int i = 0; i < 10; i++)
            {
                list1.Add(i);
                list2.Add(i);
            }

            foreach (var item in list1)
            {
                list2.Remove(item);
            }


            List<object> list3 = new List<object>();
            List<object> list4 = new List<object>();

            for (int i = 0; i < 10; i++)
            {
                object temp = new object();
                list3.Add(temp);
                list4.Add(temp);
            }

            foreach (var item in list3)
            {
                list4.Remove(item);
            }