Skip to content

sortedList.DeepClone() and immutableDictionary.DeepClone() don't compile (CS0121 ambiguous between the IDictionary and IReadOnlyDictionary overloads) #86

Description

@matt-edmondson

What's wrong

SortedList<TKey,TValue>, ImmutableDictionary<TKey,TValue> and ImmutableSortedDictionary<TKey,TValue> each implement both IDictionary<TKey,TValue> and IReadOnlyDictionary<TKey,TValue>. DeepCloneContainerExtensions has an extension for each interface (DeepClone(this IDictionary<,>) at line 148 and DeepClone(this IReadOnlyDictionary<,>) at line 268), and neither is more specific, so calling .DeepClone() directly on these types fails to compile.

#81 (commit dad2dfc) fixed this ambiguity for ConcurrentDictionary and ReadOnlyDictionary by adding concrete overloads. The three types above still have the problem. The existing ImmutableDictionary test avoids it by cloning each value by hand instead of calling the extension, so the tests don't catch it.

Reproduction

using System.Collections.Immutable;
using ktsu.DeepClone;

var a = new SortedList<string, int> { ["a"] = 1 }.DeepClone();
var b = ImmutableDictionary<string, int>.Empty.Add("a", 1).DeepClone();
var c = ImmutableSortedDictionary<string, int>.Empty.Add("a", 1).DeepClone();
error CS0121: The call is ambiguous between the following methods or properties:
'DeepCloneContainerExtensions.DeepClone<TKey, TValue>(IDictionary<TKey, TValue>)' and
'DeepCloneContainerExtensions.DeepClone<TKey, TValue>(IReadOnlyDictionary<TKey, TValue>)'

(Reproduced against the net10.0 build of current main.)

Why it matters

The README says the library clones standard collections, but a caller holding a SortedList or an immutable dictionary has to cast to one of the interfaces first. The IDictionary path then returns a plain Dictionary and loses the comparer and the sort order (tracked in #83). No cast gives back the type they started with.

Suggested fix / acceptance criteria

Activity

  1. matt-edmondson commented on Sep 28, 2026

    @matt-edmondson
    ContributorAuthor

    Triage


    Generated by Claude Code

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

Metadata

Metadata

Labels

bugSomething isn't workingreadyFully specified; implement as written

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions