From 5b26d190a6912f7a0427b90f738a6c51351ec1f6 Mon Sep 17 00:00:00 2001 From: prozolic <42107886+prozolic@users.noreply.github.com> Date: Thu, 16 Oct 2025 20:58:46 +0900 Subject: [PATCH 1/3] Improve FrozenDictionary.ToFrozenDictionary for Dictionary sources with different comparers --- .../Collections/Frozen/FrozenDictionary.cs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) 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..c2a3aaa236f080 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 @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System.Collections.Generic; @@ -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(newDictionary.Count, 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) { From 9fff89d0f272439fd746299bdaa07c36c1bc74c5 Mon Sep 17 00:00:00 2001 From: prozolic <42107886+prozolic@users.noreply.github.com> Date: Thu, 16 Oct 2025 21:33:21 +0900 Subject: [PATCH 2/3] Fix file encoding to UTF-8 with BOM --- .../src/System/Collections/Frozen/FrozenDictionary.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 c2a3aaa236f080..90f5e5a019eab6 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 @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System.Collections.Generic; From 089e68bd92668192cbebfeb9137cf80e4e6ee21f Mon Sep 17 00:00:00 2001 From: prozolic <42107886+prozolic@users.noreply.github.com> Date: Thu, 16 Oct 2025 22:48:04 +0900 Subject: [PATCH 3/3] Remove initial capacity specification --- .../src/System/Collections/Frozen/FrozenDictionary.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 90f5e5a019eab6..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 @@ -142,7 +142,7 @@ public static FrozenDictionary ToFrozenDictionary(newDictionary.Count, comparer); + var dictionary = new Dictionary(comparer); foreach (KeyValuePair pair in newDictionary) { // Dictionary's constructor uses Add, which will throw on duplicates.