Dictionary methods Remove and Clear (.NET Core) modify the collection during enumeration. No exception thrown

后端 未结 2 1960
心在旅途
心在旅途 2021-01-05 15:54

I am trying to implement a caching mechanism for enumerating collections safely, and I am checking if all modifications of the built-in collections are triggering an I

2条回答
  •  一生所求
    2021-01-05 16:31

    To clarify when the change was made available and via which methods...

    Microsoft's docs on the Dictionary's .Remove and .Clear have been updated:

    .NET Core 3.0+ only: this mutating method may be safely called without invalidating active enumerators on the Dictionary instance. This does not imply thread safety.

    .NET Core 3.0 came with C# 8.0. Since then, we have been able to modify a Dictionary during enumeration (foreach) via .Remove and .Clear only.

提交回复
热议问题