【发布时间】:2010-09-24 12:21:09
【问题描述】:
我正在尝试在循环中更新哈希表,但出现错误:System.InvalidOperationException: Collection was modified;枚举操作可能无法执行。
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;
}
有没有办法解决这个问题,或者是否有更好的数据结构用于此目的?
【问题讨论】:
-
相信这是一个 dup 问题,请参阅:stackoverflow.com/questions/287195/…
标签: c# data-structures loops hashtable