Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ToFrozenDictionary() does not adopt the IEqualityComparer of the source dictionary. #110660

Closed
JeffreyM opened this issue Dec 12, 2024 · 3 comments

Comments

@JeffreyM
Copy link

Description

I'm reporting this as a bug, though it seems too obvious to have been overlooked. If it's not a bug, I'm curious about the rationale behind ToFrozenDictionary() not adopting the IEqualityComparer from the source Dictionary.

Reproduction Steps

// Note the case insensitive IEqualityComparer<T> - StringComparer.OrdinalIgnoreCase here.
var dictionary = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase) { ["a"] = 1 };
        
// Assertions will pass
var frozenDictionary = dictionary.ToFrozenDictionary( StringComparer.OrdinalIgnoreCase);
Debug.Assert(frozenDictionary.Comparer == dictionary.Comparer);
Debug.Assert(frozenDictionary.TryGetValue("A", out int _)); 

// Assertions will fail
frozenDictionary = dictionary.ToFrozenDictionary( /* No StringComparer */);
Debug.Assert(frozenDictionary.Comparer == dictionary.Comparer);
Debug.Assert(frozenDictionary.TryGetValue("A", out int _)); 

Expected behavior

I expect the following would satisfy the assertion.

var dictionary = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase) { ["a"] = 1 };
var frozenDictionary = dictionary.ToFrozenDictionary();
Debug.Assert(frozenDictionary.Comparer == dictionary.Comparer);
Debug.Assert(frozenDictionary.TryGetValue("A", out int _)); 

Actual behavior

The actual behavior is that the assertions fail for the expected behavior above.

Regression?

No response

Known Workarounds

Explicitly pass the same IEqualityComparer to ToFrozenDictionary(comparer) as you do when instantiating a dictionary it will be called on. This is a very simple workaround. However, there are times where I would not necessarily know the IEqualityComparer of the source dictionary. Serialization/Deserialization comes to mind.

Configuration

No response

Other information

No response

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Dec 12, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-collections
See info in area-owners.md if you want to be subscribed.

@huoyaoyuan
Copy link
Member

ToXxDictionary series methods don't operate on a dictionary, they operate on collection of key-value pairs. Thus no concept of comparer will be passed implicitly.

@JeffreyM
Copy link
Author

Thank you @huoyaoyuan. That also explains why serializing a Dictionary looses the notion of it comparer. Closing this.

@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Dec 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants