class Program
{
static void Main(string[] args)
{
var dictionary = new Dictionary<string, int>()
{
{"1", 1}, {"2", 2}, {"3", 3}
};
foreach (var s in dictionary.Keys)
{
// Throws the "Collection was modified exception..." on the next iteration
// What's up with that?
dictionary[s] = 1;
}
}
}
我完全理解在枚举列表时为什么会抛出这个异常。期望在枚举过程中不会改变被枚举对象的结构是合理的。但是,改变字典中的一个值是否也会改变其结构? 特别是它的键的结构?