Fix misleading equality testing method documentation in C# objects guide #47672
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.
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 isObject.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 becauseValueType.Equals
can be overridden. The text now clarifies this is the default behavior.Changes Made
System.Object.Equals
toSystem.Object.ReferenceEquals
for identity comparisonValueType.Equals
"by default" determines field equality, rather than guaranteeing this behaviorValueType
implementation detailsThese changes maintain the beginner-friendly tone while providing technically accurate information, as requested by @BillWagner in the issue comments.
Validation
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