diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenDictionary.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenDictionary.cs index b9ddcb1b4e484f..1fbfba41561add 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenDictionary.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenDictionary.cs @@ -129,7 +129,7 @@ public static FrozenDictionary ToFrozenDictionary using the specified comparer such that all keys // are non-null and unique according to that comparer. newDictionary = source as Dictionary; - if (newDictionary is null || (newDictionary.Count != 0 && !newDictionary.Comparer.Equals(comparer))) + if (newDictionary is null) { newDictionary = new Dictionary(comparer); foreach (KeyValuePair pair in source) @@ -140,6 +140,18 @@ public static FrozenDictionary ToFrozenDictionary(comparer); + foreach (KeyValuePair pair in newDictionary) + { + // Dictionary's constructor uses Add, which will throw on duplicates. + // This implementation uses the indexer to avoid throwing and to overwrite + // existing entries such that last one wins. + dictionary[pair.Key] = pair.Value; + } + newDictionary = dictionary; + } if (newDictionary.Count == 0) {