Skip to content

Keep an OrderedDictionary's type, comparer and order when deep cloning [minor] - #134

Merged
matt-edmondson merged 1 commit into
mainfrom
claude/ordered-dictionary-clone
Nov 10, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
claude/ordered-dictionary-clone

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #108

What changed

  • New OrderedDictionary<TKey, TValue>.DeepClone() overload (under #if NET9_0_OR_GREATER). It returns new OrderedDictionary<TKey, TValue>(source.Count, source.Comparer) filled through AddClonedPairs, which keeps the type, the key order and the comparer, and removes the CS0121 ambiguity.
  • An OrderedDictionary arm in CloneDictionary. With it, the IDictionary / IReadOnlyDictionary cast paths return an OrderedDictionary with the source's comparer. A reference-equality source holding equal-but-distinct keys no longer throws.
  • The README container table and CloneDictionary's doc list the new type.

The optional ImmutableDictionary.Builder / ImmutableSortedDictionary.Builder overloads from the issue are not part of this PR.

This PR is independent of #133 (FrozenDictionary/FrozenSet, #101). Both branch from main and touch nearby code, but on separate lines, and git merge-tree confirms they merge cleanly in either order.

Tests

DeepClone.Test/OrderedDictionaryTests.cs has 5 tests: type, comparer and order kept, values deep cloned, both interface-cast paths, and reference equality.

  • Without the fix the test project doesn't compile (CS0121 ×3), which is the issue's repro.
  • Removing only the CloneDictionary arm makes the 3 interface-cast and reference-equality tests fail.
  • Full suite: 108/108 pass on net10.0. A Release build succeeds for every target framework.

Generated by Claude Code

…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
@sonarqubecloud

sonarqubecloud Bot commented Nov 7, 2026

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OrderedDictionary<K,V> (.NET 9+): .DeepClone() fails with CS0121, and the cast workarounds drop the key comparer or throw

2 participants