In .NET<5 and .NET Core 3.1 the following code
var d = new Dictionary<string, int> { { "a", 0 }, { "b", 0 }, { "c", 0 } };
foreach (var k in d.Keys)
{
d[k]+=1;
}
throws
System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
When targeting .NET 5 the snippet no longer throws.
What has changed?
I failed to find the answer in Breaking changes in .NET 5 and Performance Improvements in .NET 5.
Is it something to do with ref readonly T?