<p>I'm trying to update a hashtable in a loop but getting an error: System.InvalidOperationException: Collection was modified; enumeration operation may not execute.</p>

<pre><code>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;
}
</code></pre>

<p>Is there way around it or maybe there is a better data structure for such purpose?</p>

You cannot change the set of items stored in a collection while you are enumerating over it, since that makes life very difficult for the iterator in most cases.  Consider the case where the collection represents a balanced tree, and may well undergo rotations after an insert.  The enumerate would have no plausible way of keeping track of what it has seen.
However, if you are just trying to update the value then you can write:
```
```
deEntry.Value = sValue
```
```
Updating the value here has no impact on the enumerator.