Skip to content

Update GetHashCode implementations to use HashCode.Combine#52121

Open
Copilot wants to merge 7 commits intomainfrom
copilot/review-gethashcode-implementations
Open

Update GetHashCode implementations to use HashCode.Combine#52121
Copilot wants to merge 7 commits intomainfrom
copilot/review-gethashcode-implementations

Conversation

Copy link
Contributor

Copilot AI commented Mar 9, 2026

  • Analyzed the codebase to find GetHashCode implementations using outdated patterns (XOR, Tuple) that should use HashCode.Combine
  • Update xor2.cs (C#): Replace Tuple.Create(x, y).GetHashCode() with HashCode.Combine(x, y)
  • Update xor2.vb (VB): Replace Tuple.Create(x, y).GetHashCode() with HashCode.Combine(x, y)
  • Update xor2.fs (F#): Replace tuple hash with System.HashCode.Combine(x, y)
  • Update system-object-gethashcode.md: Updated article text to recommend HashCode.Combine; removed version reference per reviewer suggestion; updated shift1 section to say "In .NET Framework" per reviewer suggestion
  • Update equals2.cs (C#): Update Point2 and Point3D.GetHashCode to use HashCode.Combine
  • Update equals2.vb (VB): Update Point1 and Point3D.GetHashCode to use HashCode.Combine
  • Update equals2.fs (F#): Update Point and Point3D.GetHashCode to use HashCode.Combine
  • Update C# covariance article: Update inline PersonComparer.GetHashCode to use HashCode.Combine
  • Update VB covariance article: Update inline PersonComparer.GetHashCode to use HashCode.Combine
  • Add missing Project.fsproj in F# Equals snippets folder to fix CI build
  • Add missing Project.fsproj in F# GetHashCode snippets folder to fix CI build
Original prompt

This section details on the original issue you should resolve

<issue_title>Review GetHashCode implementations</issue_title>
<issue_description>Starting with .NET Core 3.1, HashCode.Combine APIs existed and should be the recommended way to implement GetHashCode method.

This is one example where the new implementation can be used.

The same applies to dotnet-api-docs repo, should I open another issue there?

Comments on the Issue (you are @copilot in this section)

@gewarren I think it's fine to track this in one place, even for the dotnet-api-docs repo.

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.


Internal previews

📄 File 🔗 Preview link
docs/csharp/programming-guide/concepts/covariance-contravariance/using-variance-in-interfaces-for-generic-collections.md Using Variance in Interfaces for Generic Collections (C#)
docs/fundamentals/runtime-libraries/system-object-gethashcode.md System.Object.GetHashCode method
docs/visual-basic/programming-guide/concepts/covariance-contravariance/using-variance-in-interfaces-for-generic-collections.md Using Variance in Interfaces for Generic Collections (Visual Basic)

Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com>
Copilot AI changed the title [WIP] Update GetHashCode implementations to use HashCode.Combine Update GetHashCode implementations to use HashCode.Combine Mar 9, 2026
@BillWagner
Copy link
Member

Tagging @T-Gro

Do you want the F# samples here to use HashCode.Combine? We recommend it for C# and VB. What's the F# community consensus here?

If not, I'll have Copilot reverse those changes.

@T-Gro
Copy link
Member

T-Gro commented Mar 10, 2026

In F# the need to do custom HashCode is quite rare, using the .Combine helpers to put together two hash codes is OK here.

Copy link
Member

@BillWagner BillWagner left a comment

Choose a reason for hiding this comment

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

With a couple suggestions, this is ready for final review

@BillWagner BillWagner marked this pull request as ready for review March 10, 2026 15:28
@BillWagner BillWagner requested a review from a team as a code owner March 10, 2026 15:28
Copilot AI review requested due to automatic review settings March 10, 2026 15:28
@BillWagner
Copy link
Member

@copilot You need to add a basic fsproj file in the /docs/fundamentals/runtime-libraries/snippets/System/Object/Equals/fsharp folder.

That will fix the CI build.

Copy link
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

Updates GetHashCode guidance and snippets across the docs repo to use System.HashCode.Combine instead of older, collision-prone or allocation-heavy patterns.

Changes:

  • Replaces tuple-based hashing and shift/XOR patterns in GetHashCode examples with HashCode.Combine.
  • Updates System.Object.GetHashCode article text to recommend HashCode.Combine and positions shift-and-wrap as a .NET Framework-era alternative.
  • Simplifies PersonComparer.GetHashCode in C# and Visual Basic variance articles to use HashCode.Combine.

Reviewed changes

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

Show a summary per file
File Description
docs/visual-basic/programming-guide/concepts/covariance-contravariance/using-variance-in-interfaces-for-generic-collections.md Updates VB comparer hash logic to HashCode.Combine.
docs/fundamentals/runtime-libraries/system-object-gethashcode.md Updates guidance to recommend HashCode.Combine and reframes shift-and-wrap for .NET Framework.
docs/fundamentals/runtime-libraries/snippets/System/Object/GetHashCode/vb/xor2.vb Switches tuple hashing to HashCode.Combine and updates output note.
docs/fundamentals/runtime-libraries/snippets/System/Object/GetHashCode/fsharp/xor2.fs Switches tuple hashing to HashCode.Combine and updates output note.
docs/fundamentals/runtime-libraries/snippets/System/Object/GetHashCode/csharp/xor2.cs Switches tuple hashing to HashCode.Combine and updates output note.
docs/fundamentals/runtime-libraries/snippets/System/Object/Equals/vb/equals2.vb Updates Point/Point3D hash logic to HashCode.Combine.
docs/fundamentals/runtime-libraries/snippets/System/Object/Equals/fsharp/equals2.fs Updates Point/Point3D hash logic to HashCode.Combine.
docs/fundamentals/runtime-libraries/snippets/System/Object/Equals/csharp/equals2.cs Updates Point/Point3D hash logic to HashCode.Combine.
docs/csharp/programming-guide/concepts/covariance-contravariance/using-variance-in-interfaces-for-generic-collections.md Updates C# comparer hash logic to HashCode.Combine.

Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Mar 10, 2026

@copilot You need to add a basic fsproj file in the /docs/fundamentals/runtime-libraries/snippets/System/Object/Equals/fsharp folder.

That will fix the CI build.

Added Project.fsproj in commit f13263e. The project includes all 8 .fs files in that folder and builds successfully.

@BillWagner
Copy link
Member

@copilot One more fsproj file to add in this folder: docs/fundamentals/runtime-libraries/snippets/System/Object/GetHashCode/fsharp/

Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Mar 10, 2026

@copilot One more fsproj file to add in this folder: docs/fundamentals/runtime-libraries/snippets/System/Object/GetHashCode/fsharp/

Added Project.fsproj for the GetHashCode/fsharp folder in commit 67ad368. It includes all 4 .fs files and builds successfully.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Review GetHashCode implementations

4 participants