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;
}
}
}
我完全理解为什么在枚举列表时会抛出这个异常。期望在枚举期间枚举对象的结构不会改变似乎是合理的。但是,更改字典的值是否也会更改其结构?具体来说,它的键的结构?