Fix Distinct with custom comparer (hash bug) #1078
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There is a bug in .Distinct() when a custom comparer is used. PR includes test and fix.
Bug is that default GetHashCode is used -- this is not valid with a custom (key) comparer.
First both commits only fix one occurence of the bug (there are more). Some things come to my mind (before adding more tests/fixes):
Distinct<EQ, T, K>(...)
variant which I would prefer for my use case (avoiding runtime comparer).Func<string,string,bool>
for helper functions like this (comparer). This would avoid hiding a gethashcode "problem" (either needs to be supplied or otherwise risks performance penalty). Should be enough to allow a variant with IEqualityComparer parameter and make something likeEqCompare<T>
public to allow on-the-fly creation of Comparers using equals+gethashcode.PS: In case you like to stay with 'comparer func parameter': Maybe code (and bugfix) should be improved by moving
IfNone
andMatch
outside to avoid calling this within each call of of the comparer. Maybecomparer
the should be non-optional (adding another signature without comparer) to clean up code.