.NET 2
The standard:
foreach(KeyValuePair<int,int> entry in MyDic)
{
entry.Value += i; // does not work :(
i++;
}
Recommendations?
.NET 2
The standard:
foreach(KeyValuePair<int,int> entry in MyDic)
{
entry.Value += i; // does not work :(
i++;
}
Recommendations?
Loop over the Keys
property instead. When you use enumerators, any modification to the underlying collection invalidates the enumerator when it next calls MoveNext
.
You can enumerate the Keys
or AllKeys
(which is just a string array) and modify the values they point to quite safely.
Also, the properties of KeyValuePair<K, V>
are themselves read-only, so, iterator blocks or not, there's no circumstances under which you could modify those values without abusing reflection.