Skip to content

Fix misleading equality testing method documentation in C# objects guide #47672

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

Merged
merged 2 commits into from
Aug 4, 2025

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 30, 2025

This PR addresses misleading information about equality testing methods in the C# objects documentation that could confuse developers about when to use different equality comparison methods.

Issues Fixed

1. Incorrect recommendation for identity comparison
The documentation incorrectly suggested using Object.Equals to determine if two class instances refer to the same location in memory (identity comparison). However, Object.Equals can be overridden and doesn't guarantee reference equality. The correct method for identity comparison is Object.ReferenceEquals.

2. Misleading guarantees about ValueType.Equals behavior
The documentation stated that ValueType.Equals determines whether instance fields have the same values, but this is misleading because ValueType.Equals can be overridden. The text now clarifies this is the default behavior.

Changes Made

  • Line 37: Changed System.Object.Equals to System.Object.ReferenceEquals for identity comparison
  • Line 38: Modified text to clarify that ValueType.Equals "by default" determines field equality, rather than guaranteeing this behavior
  • Line 42: Added "default" qualifier to the description of ValueType implementation details

These changes maintain the beginner-friendly tone while providing technically accurate information, as requested by @BillWagner in the issue comments.

Validation

  • ✅ Code snippets compile and run correctly
  • ✅ Markdown linting passes
  • ✅ Changes are minimal and surgical

Fixes #29582.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.


Internal previews

📄 File 🔗 Preview link
docs/csharp/fundamentals/object-oriented/objects.md docs/csharp/fundamentals/object-oriented/objects

Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Misleading notes about equality testing methods Fix misleading equality testing method documentation in C# objects guide Jul 30, 2025
@Copilot Copilot AI requested a review from BillWagner July 30, 2025 19:44
Copilot finished work on behalf of BillWagner July 30, 2025 19:44
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.

This LGTM and is ready for final review.

@BillWagner BillWagner marked this pull request as ready for review July 31, 2025 15:50
@BillWagner BillWagner requested a review from a team as a code owner July 31, 2025 15:50
@BillWagner BillWagner requested a review from tdykstra July 31, 2025 15:50
@dotnet-policy-service dotnet-policy-service bot added the okr-quality Content-quality KR: Concerns article defects (bugs), freshness, or build warnings. label Jul 31, 2025
@BillWagner BillWagner merged commit 6af8c82 into main Aug 4, 2025
15 checks passed
@BillWagner BillWagner deleted the copilot/fix-29582 branch August 4, 2025 18:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dotnet-csharp/svc fundamentals/subsvc okr-quality Content-quality KR: Concerns article defects (bugs), freshness, or build warnings.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Misleading notes about equality testing methods
3 participants