I’m trying to update a hashtable in a loop but getting an error: System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
private Hashtable htSettings_m = new Hashtable(); htSettings_m.Add('SizeWidth', '728'); htSettings_m.Add('SizeHeight', '450'); string sKey = ''; string sValue = ''; foreach (DictionaryEntry deEntry in htSettings_m) { // Get value from Registry and assign to sValue. // ... // Change value in hashtable. sKey = deEntry.Key.ToString(); htSettings_m[sKey] = sValue; }
Is there way around it or maybe there is a better data structure for such purpose?
you could read the collection of keys into another IEnumerable instance first, then foreach over that list