Today, when I use the foreach statement to iterate over the dictionary, I find an error:
The collection has been modified; enumeration operations may not be performed
Search the web, and find that foreach is iterative, and it cannot be added and deleted. But, I tried, and found that there was another problem: when comparing and correcting values, it also gives an error, such as:
1 foreach(keyvaluepair<string,Double> soilstrkvinchsoil)2 {3 if(Isvalidval (soilstrkv.value))4 {5Soil[soilstrkv.key] =Replaceinvalidval;6 //sitenumstr = soilStrKV.Key.Substring (0, SoilStrKV.Key.IndexOf ('_'));7 //Yearstr8 //Sitesoil.add ();9 }Ten } One A /// <summary> - ///False indicates a non-conforming value, otherwise conforming. - /// </summary> the /// <param name= "inval" ></param> - /// <returns></returns> - Private BOOLIsvalidval (Doubleinval) - { + returnInval <=Available_val; -}
In line 5th, the error is corrected (when the duplicate value is modified).
The C#-collection has been modified; enumeration operations may not be performed