Skip to content

Keep a ConcurrentDictionary's type and comparer on the netstandard builds [patch] - #127

Merged
matt-edmondson merged 2 commits into
mainfrom
fix/125-concurrent-dictionary-netstandard
Nov 11, 2026
Merged

matt-edmondson merged 2 commits into
mainfrom
fix/125-concurrent-dictionary-netstandard

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

On the netstandard2.0/netstandard2.1 builds, which .NET 5–7 apps, .NET Framework, Unity and Mono restore, CloneDictionary had no ConcurrentDictionary arm, because the arm sat under #if NET6_0_OR_GREATER. A ConcurrentDictionary cloned through IDictionary/IReadOnlyDictionary therefore came back as a plain Dictionary with the default comparer, and the typed DeepClone(this ConcurrentDictionary<,>) also dropped the comparer. A reference-equality source holding equal keys threw ArgumentException on clone.

Changes

  • The ConcurrentDictionary arm now applies on every target.
  • A new ConcurrentComparer<TKey, TValue> helper supplies the comparer to both CloneDictionary and the typed overload:
    • On .NET it reads ConcurrentDictionary.Comparer directly.
    • On netstandard it looks that property up once by reflection, cached per closed type the way WrappedDictionary does, so a .NET 6+ runtime keeps the comparer. Only runtimes without the property fall back to EqualityComparer<TKey>.Default.
  • The remarks no longer limit comparer preservation to ".NET 6 and later" builds.
  • The CLAUDE.md target list now matches what the SDK builds: net10/9/8 and netstandard2.1/2.0.

Verification

The test project targets only net10.0 and binds to the net10 build, so it never runs the netstandard code. Pointing it at a netstandard build doesn't compile today: the immutable-collection tests hit overload ambiguity, and netstandard2.0 lacks nullability attributes, which triggers CA1062. Instead I checked with a scratch console harness that references the library with SetTargetFramework=netstandard2.0 / netstandard2.1. I confirmed the loaded assembly's TargetFramework attribute in each case.

The harness ran the three tests the issue names, ConcurrentDictionary_ThroughIDictionary_KeepsComparer, ConcurrentDictionary_WithReferenceEquality_ClonesEqualKeys and ConcurrentDictionary_DeepClone_ShouldKeepComparer, plus the IReadOnlyDictionary path and the typed reference-equality case.

Build Without this change With this change
netstandard2.0 5/5 fail (two throw ArgumentException) 5/5 pass
netstandard2.1 5/5 fail 5/5 pass
net10.0 n/a 5/5 pass

The full DeepClone.Test suite passes on net10.0: 99/99.

Not included: a CI leg that runs the tests against the netstandard builds, which the triage suggests. It first needs the test project to compile against netstandard (the immutable-overload and CA1062 issues above), so it is worth its own issue.

Fixes #125


Generated by Claude Code

…ilds [patch]

CloneDictionary had a ConcurrentDictionary arm only under NET6_0_OR_GREATER,
so on the netstandard2.0/2.1 builds (which .NET 5-7 apps, .NET Framework,
Unity and Mono restore) a ConcurrentDictionary cloned through IDictionary or
IReadOnlyDictionary came back as a plain Dictionary with the default
comparer, and the typed overload dropped the comparer too. A
reference-equality source with equal keys threw on clone.

The arm now applies on every target. The comparer comes from
ConcurrentDictionary.Comparer on .NET, and on netstandard from a cached
run-time lookup of that property, falling back to the default comparer only
on runtimes that do not have it. Correct the remarks and the CLAUDE.md
target list.

Fixes #125

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW3UyxP3KPcGGow8Vry
@sonarqubecloud

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

2 participants