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;
}
}
}
リストを列挙するときにこの例外がスローされる理由を完全に理解しています。列挙中に、列挙されたオブジェクトの構造が変更されないことを期待するのは合理的と思われます。ただし、辞書の値を変更すると、その構造も変更されますか?具体的には、そのキーの構造は?