Let concurrent and read-only dictionaries call DeepClone() without a cast - #84
Merged
matt-edmondson merged 1 commit intoSep 27, 2026
Merged
Conversation
…cast ConcurrentDictionary<,> and ReadOnlyDictionary<,> implement both IDictionary<,> and IReadOnlyDictionary<,>, so dictionary.DeepClone() was an ambiguous call (CS0121) for them. Add concrete overloads, as Dictionary<,> and SortedDictionary<,> already have. The ConcurrentDictionary overload keeps the key comparer on .NET 6 and later, where the type exposes it. Remove the cast workarounds from the tests. Fixes #81 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KaXGMeYeXB1J32kSrLGhgJ
|
matt-edmondson
deleted the
fix/81-concurrent-readonly-dictionary-deepclone
branch
September 27, 2026 15:10
This was referenced Sep 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes #81
What was wrong
ConcurrentDictionary<,>andReadOnlyDictionary<,>implement bothIDictionary<,>andIReadOnlyDictionary<,>, so callingDeepClone()on either one failed to compile with CS0121 (ambiguous call). #79 already fixed this forDictionary<,>andSortedDictionary<,>. These two types were still left.Change
DeepClone(this ConcurrentDictionary<,>)overload that returns aConcurrentDictionary<,>. On .NET 6 and later it keeps the source's key comparer. The .NET Standard targets can't readConcurrentDictionary.Comparer, so on those the clone uses the default comparer. The XML doc says so.DeepClone(this ReadOnlyDictionary<,>)overload that returns a newReadOnlyDictionary<,>wrapping a deep-cloned dictionary.DeepCloneTests.cs("Cast to IDictionary to resolve ambiguity") andSpecializedCollectionTests.cs("explicit cast to IDictionary to resolve ambiguity").Tests
ConcurrentDictionary_DeepClone_ShouldCreateIndependentCopynow callsoriginal.DeepClone()directly.ConcurrentDictionary_DeepClone_ShouldKeepComparerandReadOnlyDictionaryWrapper_DeepClone_ShouldCreateIndependentCopy.dotnet testpasses 44/44.Not in scope
ImmutableDictionary<,>andImmutableSortedDictionary<,>also implement both interfaces, so they would hit the same ambiguity. Cloning them throughIDictionary<,>would also callAddon an immutable type. They need their own design, so they are left for a separate issue. #83 (comparer loss through theIDictionarypath forSortedListand others) is also separate.🤖 Generated with Claude Code
https://claude.ai/code/session_01KaXGMeYeXB1J32kSrLGhgJ
Generated by Claude Code