Repository navigation
Keep an OrderedDictionary's type, comparer and order when deep cloning [minor] - #134
Merged
Merged
Conversation
…g [minor] OrderedDictionary (.NET 9+) implements both IDictionary and IReadOnlyDictionary, so orderedDictionary.DeepClone() was ambiguous (CS0121), and the interface-cast paths fell through to a plain Dictionary with the default comparer, which also threw for a reference-equality source holding equal keys. Add an OrderedDictionary overload and a CloneDictionary arm that keep the comparer and the key order. Fixes #108 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017vfJW2kWKScgik3tRTLE8X
|
This was referenced Nov 16, 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 #108
What changed
OrderedDictionary<TKey, TValue>.DeepClone()overload (under#if NET9_0_OR_GREATER). It returnsnew OrderedDictionary<TKey, TValue>(source.Count, source.Comparer)filled throughAddClonedPairs, which keeps the type, the key order and the comparer, and removes the CS0121 ambiguity.OrderedDictionaryarm inCloneDictionary. With it, theIDictionary/IReadOnlyDictionarycast paths return anOrderedDictionarywith the source's comparer. A reference-equality source holding equal-but-distinct keys no longer throws.CloneDictionary's doc list the new type.The optional
ImmutableDictionary.Builder/ImmutableSortedDictionary.Builderoverloads from the issue are not part of this PR.This PR is independent of #133 (FrozenDictionary/FrozenSet, #101). Both branch from
mainand touch nearby code, but on separate lines, andgit merge-treeconfirms they merge cleanly in either order.Tests
DeepClone.Test/OrderedDictionaryTests.cshas 5 tests: type, comparer and order kept, values deep cloned, both interface-cast paths, and reference equality.CloneDictionaryarm makes the 3 interface-cast and reference-equality tests fail.Generated by Claude Code