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

Dictionary equal and hashing in C# #2126

Closed
bernardnormier opened this issue May 7, 2024 · 2 comments
Closed

Dictionary equal and hashing in C# #2126

bernardnormier opened this issue May 7, 2024 · 2 comments
Milestone

Comments

@bernardnormier
Copy link
Member

In C#, we check if two dictionaries are equal using:

public static bool DictionaryEquals(IDictionary d1, IDictionary d2)

The algorithm assumes the two dictionaries are sorted in the same order. This is not the behavior you would expect since it depends on the IDictionary implementation. Two implementations with the same contents can easily compare different.

IceRPC C# provides an order-independent implementation of DictionaryEqual:
https://github.com/icerpc/icerpc-csharp/blob/57995b702436169798fc3fe5fd513e6f09591397/src/IceRpc/Internal/DictionaryExtensions.cs#L10

Ice C# also provides HashCode computation for these dictionaries that is also order-dependent (that's consistent with Equal, which is good). IceRPC doesn't provide a HashCode implementation. It's not obvious how to provide a correct order-independent HashCode implementation. A simple solution is to hash the size of the dictionary, not its contents.

@bernardnormier
Copy link
Member Author

Another option is to do like Java: the hash code for a Map / Set is the sum of the hash code of all elements:
https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Set.html#hashCode()

@bernardnormier
Copy link
Member Author

Fixed: we no longer use "entry hashing" for any dictionary in C#.

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

1 participant