Skip to content

Implement EqualityComparer<T>.Create with key selector parameters#125024

Merged
eiriktsarpalis merged 4 commits into
dotnet:mainfrom
weitzhandler:eq-comparer-issue-115797
May 18, 2026
Merged

Implement EqualityComparer<T>.Create with key selector parameters#125024
eiriktsarpalis merged 4 commits into
dotnet:mainfrom
weitzhandler:eq-comparer-issue-115797

Conversation

@weitzhandler
Copy link
Copy Markdown
Contributor

Fixes #115797

Copilot AI review requested due to automatic review settings March 1, 2026 05:57
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Mar 1, 2026
@weitzhandler weitzhandler force-pushed the eq-comparer-issue-115797 branch from 5ba9d15 to 55f807c Compare March 1, 2026 05:58
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 keySelector and basic equality behavior with/without a custom key comparer.
  • Updated System.Collections reference 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.

@weitzhandler weitzhandler force-pushed the eq-comparer-issue-115797 branch from 55f807c to 6ccb5b5 Compare March 1, 2026 06:03
Copilot AI review requested due to automatic review settings March 1, 2026 06:09
@weitzhandler weitzhandler force-pushed the eq-comparer-issue-115797 branch from 6ccb5b5 to 0ca8025 Compare March 1, 2026 06:09
@weitzhandler weitzhandler force-pushed the eq-comparer-issue-115797 branch from 0ca8025 to 02b5e71 Compare March 1, 2026 06:12
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

Comment thread src/libraries/System.Collections/ref/System.Collections.cs Outdated
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

@ViveliDuCh
Copy link
Copy Markdown
Member

Thanks for working on this! :)

I noticed that the API review also discussed adding a Comparer<T>.Create<TKey>() overload alongside this for symmetry. Would you consider including it as well?

@dotnet-policy-service dotnet-policy-service Bot added no-recent-activity and removed needs-author-action An issue or pull request that requires more info or actions from the author. no-recent-activity labels Apr 24, 2026
Copilot AI review requested due to automatic review settings April 24, 2026 11:29
@weitzhandler weitzhandler force-pushed the eq-comparer-issue-115797 branch from 5d16bfc to 7bae40c Compare April 24, 2026 11:29
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

weitzhandler and others added 3 commits May 15, 2026 16:03
…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>
Copilot AI review requested due to automatic review settings May 18, 2026 13:14
@eiriktsarpalis eiriktsarpalis enabled auto-merge (squash) May 18, 2026 13:14
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

@eiriktsarpalis eiriktsarpalis merged commit 7c254b3 into dotnet:main May 18, 2026
148 of 152 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Collections community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[API Proposal]: EqualityComparer.Create from selecting a key property

4 participants