Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public static FrozenDictionary<TKey, TElement> ToFrozenDictionary<TSource, TKey,
// Ensure we have a Dictionary<,> using the specified comparer such that all keys
// are non-null and unique according to that comparer.
newDictionary = source as Dictionary<TKey, TValue>;
if (newDictionary is null || (newDictionary.Count != 0 && !newDictionary.Comparer.Equals(comparer)))
if (newDictionary is null)
{
newDictionary = new Dictionary<TKey, TValue>(comparer);
foreach (KeyValuePair<TKey, TValue> pair in source)
Expand All @@ -140,6 +140,18 @@ public static FrozenDictionary<TKey, TElement> ToFrozenDictionary<TSource, TKey,
newDictionary[pair.Key] = pair.Value;
}
}
else if (newDictionary.Count != 0 && !newDictionary.Comparer.Equals(comparer))
{
var dictionary = new Dictionary<TKey, TValue>(comparer);
foreach (KeyValuePair<TKey, TValue> 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)
{
Expand Down
Loading