Runtime: Clarify Dictionary behavior/guarantees around mutation during enumeration

Created on 25 Oct 2019  路  6Comments  路  Source: dotnet/runtime

We've traced an EF Core issue to a behavioral difference between .NET Framework and Core, and I'd like to be sure we have the right picture.

The scenario in question was enumerating the Dictionary's values and removing entries during that enumeration. While in certain cases .NET Framework throws InvalidOperationException ("Collection was modified"), .NET Core does not ever seem to do so. It's worth emphasizing that no concurrency is involved - a single thread is doing all the operations. A cursory look at Dictionary.cs shows that ConcurrentOperationsNotSupported exceptions are thrown only from mutating methods, but enumerators don't seem to throw them.

  1. Is it correct to say that the current implementation of Dictionary supports non-concurrent mutation during iteration?
  2. More strongly, is this something that can be depended on going forward (I think I know the answer :)).

/cc @stephentoub @jkotas @benaadams @ajcvickers

area-System.Collections documentation

Most helpful comment

Is it correct to say that the current implementation of Dictionary supports non-concurrent mutation during iteration?

Only removal. This was enabled as a feature in https://github.com/dotnet/coreclr/pull/18854.

is this something that can be depended on going forward

Yes.

We should ensure the docs are updated to reflect this.

All 6 comments

Is it correct to say that the current implementation of Dictionary supports non-concurrent mutation during iteration?

Only removal. This was enabled as a feature in https://github.com/dotnet/coreclr/pull/18854.

is this something that can be depended on going forward

Yes.

We should ensure the docs are updated to reflect this.

Great, thanks!

@roji do you want to open an issue (or PR) against https://github.com/dotnet/dotnet-api-docs/ ? That's the place for it.

Yep @danmosemsft, will do that soon.

OK, let's close this then. Thanks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Timovzl picture Timovzl  路  3Comments

sahithreddyk picture sahithreddyk  路  3Comments

yahorsi picture yahorsi  路  3Comments

jzabroski picture jzabroski  路  3Comments

matty-hall picture matty-hall  路  3Comments