I know this goes against the .NET rules, but sometimes I need it. For example, I need to run through a Dictionary<string, bool>. This dictionary stores my variables in a parsed logic equation. I want to output a truth table, so I need to iterate through and set elements.
One thing I’ve tried is
foreach (var x in Variables.Keys)
{
bool on = ((in) & (j << in)) > 0;
Variables[x] = on;
builder.Append(on == true ? '1' : '0').Append('\t');
j++;
}
I just get a InvalidOperationException: Collection was modified; enumeration operation may not execute. I converted the dictionary to an array and tried to modify it that way, but KeyValuePair.Value is readonly, so it won’t work.
You could create a copy of the key collection: