Implement EqualityComparer<T>.Create with key selector parameters#125024
Merged
eiriktsarpalis merged 4 commits intoMay 18, 2026
Conversation
5ba9d15 to
55f807c
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a new factory overload to System.Collections.Generic.EqualityComparer<T> that builds an EqualityComparer<T> from a key selector (and optional key comparer), enabling common “compare by property” scenarios (per #115797).
Changes:
- Added
EqualityComparer<T>.Create<TKey>(Func<T?, TKey?> keySelector, IEqualityComparer<TKey>? keyComparer = null)in CoreLib. - Added unit tests covering null
keySelectorand basic equality behavior with/without a custom key comparer. - Updated
System.Collectionsreference assembly surface area to include the new overload.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/libraries/System.Private.CoreLib/src/System/Collections/Generic/EqualityComparer.cs | Introduces the new Create<TKey> overload and wires it through the existing delegate-based comparer implementation. |
| src/libraries/System.Collections/tests/Generic/Comparers/EqualityComparer.Tests.cs | Adds tests for the new overload’s argument validation and Equals behavior. |
| src/libraries/System.Collections/ref/System.Collections.cs | Updates the reference contract to expose the new API. |
55f807c to
6ccb5b5
Compare
6ccb5b5 to
0ca8025
Compare
0ca8025 to
02b5e71
Compare
02b5e71 to
3588128
Compare
3588128 to
34270f6
Compare
This was referenced Mar 1, 2026
Member
|
Thanks for working on this! :) I noticed that the API review also discussed adding a |
ViveliDuCh
reviewed
Apr 10, 2026
5d16bfc to
7bae40c
Compare
This was referenced Apr 24, 2026
Open
eiriktsarpalis
approved these changes
Apr 27, 2026
…ash code test assertions - Remove null short-circuit in GetHashCode that bypassed keySelector, ensuring consistency with Equals which always passes null through keySelector (per reviewer feedback from eiriktsarpalis). - Add GetHashCode assertions to EqualityComparerCreate_KeySelectorUsed test to verify equal items produce equal hash codes. - Add GetHashCode assertions to EqualityComparerCreate_KeySelectorComparerUsed test including null-handling scenarios, verifying the equality/hash code contract holds for all cases. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- KeySelectorPassesNullToSelector: verifies null flows to keySelector in both Equals and GetHashCode (not short-circuited), with call counting. - KeySelectorReturnsNullKey: verifies correct behavior when keySelector maps an input to a null key (GetHashCode returns 0, Equals uses key-level null comparison). - KeySelectorNotHandlingNull_Throws: verifies NullReferenceException propagates when keySelector doesn't guard against null. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
dc9ce64 to
2c90ea3
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #115797