Keep the key comparer when deep cloning a dictionary - #79
Merged
Merged
Conversation
The IDictionary and IReadOnlyDictionary overloads always built a Dictionary with the default comparer. A cloned case-insensitive dictionary stopped finding keys, and a SortedDictionary came back unordered. They now build a dictionary of the same kind, with the same comparer, when the runtime type is a Dictionary or a SortedDictionary. New concrete Dictionary and SortedDictionary overloads keep the comparer too. Because both types implement IDictionary and IReadOnlyDictionary, these overloads also let dictionary.DeepClone() compile without a cast (CS0121). Fixes #77 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NvZoJBSka1emZMaEXy4Cfj
|
This was referenced Sep 26, 2026
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 #77
What was wrong
The
IDictionaryandIReadOnlyDictionaryoverloads ofDeepClone()usedToDictionary, which always builds aDictionarywith the default comparer:OrdinalIgnoreCasedictionary no longer found"key".SortedDictionarycame back as an unorderedDictionary.Separately,
dict.DeepClone()on aDictionary<,>didn't compile (CS0121), because it matches both interface overloads equally well.Change
CloneDictionaryhelper.SortedDictionary<,>, it builds aSortedDictionarywith the same comparer.Dictionary<,>, it builds aDictionarywith the same comparer.Dictionary, as before.Dictionary<TKey,TValue> DeepClone(this Dictionary<TKey,TValue>)andSortedDictionary<TKey,TValue> DeepClone(this SortedDictionary<TKey,TValue>).[minor]because it adds public API. Existing call sites that cast to an interface behave the same apart from keeping the comparer.Tests
New tests in
SpecializedCollectionTests:Dictionary_DeepCloneAsIDictionary_KeepsComparerandDictionary_DeepCloneAsIReadOnlyDictionary_KeepsComparer: a case-insensitive lookup still works after cloning.SortedDictionary_DeepCloneAsIDictionary_KeepsTypeAndComparer: the clone is still sorted, with a custom descending comparer.Dictionary_DeepClone_WithoutCast_KeepsComparerAndClonesValues,SortedDictionary_DeepClone_WithoutCast_KeepsComparerandDictionary_DeepClone_Null_ShouldThrow: the concrete overloads.Verification
main, the three interface tests fail (ContainsKey("key")is false, and the clone's type isDictionary).main, the three concrete-call tests don't compile, with CS0121 between the two interface overloads.netstandard2.0included, has no warnings.🤖 Generated with Claude Code
https://claude.ai/code/session_01NvZoJBSka1emZMaEXy4Cfj
Generated by Claude Code