Skip to content

netstandard2.0/2.1: immutableDictionary.DeepClone() fails to compile (CS0121), and the cast workaround returns a plain Dictionary without the key comparer #116

Open

Description

@matt-edmondson

What's wrong

#86 and #98 fixed the CS0121 ambiguity and the lost key comparer for ImmutableDictionary and ImmutableSortedDictionary, but only for the .NET builds. All of the immutable handling is behind #if NET:

  • the typed overloads in DeepClone/DeepCloneContainerExtensions.cs L284-L338
  • the immutable cases in CloneDictionary at L450-L454

The package also ships netstandard2.0 and netstandard2.1 builds (ktsu.Sdk TFMs: net10.0, net9.0, net8.0, netstandard2.0, netstandard2.1). DeepClone.csproj references System.Memory and System.Threading.Tasks.Extensions for those targets, but not System.Collections.Immutable. Consumers on .NET Framework, Unity or Mono still hit both of the original problems:

  1. Compile error. immutableDict.DeepClone() and immutableSortedDict.DeepClone() fail with CS0121, because both types implement both IDictionary<,> and IReadOnlyDictionary<,>.
  2. Silent data change. Casting to either interface to get past the error falls through to _ => new Dictionary<TKey, TValue>() (L455). The clone loses the key comparer, and an ImmutableSortedDictionary comes back as an unsorted, mutable Dictionary.

The README table (L612-L613) marks those overloads "(.NET only)". It doesn't mention that the plain call fails to compile on netstandard, or that the cast workaround drops the comparer.

Repro

1. In a netstandard2.0 class library with a ProjectReference to DeepClone.csproj and System.Collections.Immutable 8.0.0:

var d = ImmutableDictionary.Create<string,int>(StringComparer.OrdinalIgnoreCase).Add("Key", 1);
var c1 = d.DeepClone();
var s = ImmutableSortedDictionary.Create<string,int>(StringComparer.OrdinalIgnoreCase).Add("Key", 1);
var c2 = s.DeepClone();
error CS0121: The call is ambiguous between 'DeepClone<TKey, TValue>(IDictionary<TKey, TValue>)' and 'DeepClone<TKey, TValue>(IReadOnlyDictionary<TKey, TValue>)'

2. Calling the netstandard2.0 ktsu.DeepClone.dll through the interface casts:

ImmutableDictionary via IDictionary -> Dictionary`2, ContainsKey("KEY")=False (source: True)
via IReadOnlyDictionary -> ContainsKey("KEY")=False

Suggested fix / acceptance criteria

  • Add a conditional PackageReference to System.Collections.Immutable for the netstandard TFMs, the same way System.Memory is referenced.
  • Widen the #if NET guards around the immutable overloads and the CloneDictionary cases so the netstandard builds get them too.
  • If the extra dependency isn't wanted, document the CS0121 and the comparer loss next to the "(.NET only)" rows instead.
  • Done when:
    • a netstandard2.0 consumer compiles immutableDict.DeepClone() and immutableSortedDict.DeepClone();
    • the clones keep their KeyComparer;
    • the comparer tests pass against the netstandard2.0 build.

Activity

  1. added
    bug
    on Nov 1, 2026
  2. matt-edmondson commented on Nov 1, 2026

    @matt-edmondson
    ContributorAuthor

    Triage

    • Category: Bug
    • Priority: High
    • Area: netstandard2.0/2.1 builds: the #if NET guards in DeepClone/DeepCloneContainerExtensions.cs (L284-L338, L450-L454) and the netstandard package references in DeepClone.csproj
    • Suggested assignment: DeepClone maintainer. No team mapping is set for this area.

    Assessment: There are two problems on a shipped target:

    1. A plain DeepClone() call on an immutable dictionary fails to compile (CS0121).
    2. The obvious cast workaround compiles but silently returns a mutable, unsorted Dictionary without the key comparer.

    The second is a silent data change: lookups that should succeed case-insensitively return false. I've rated it High rather than Medium because of that. .NET Framework, Unity and Mono consumers get no warning. The preferred fix is to add a conditional System.Collections.Immutable PackageReference and widen the guards. If the extra dependency isn't wanted, the fallback is to document both problems.

    Related / duplicates:


    Generated by Claude Code

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions