Fix incorrect protected access modifier documentation and code comments #47038
+3
−3
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.
The documentation for the
protected
keyword contained two significant errors that provided misleading information about C# protected member access rules.Issues Fixed
1. Incorrect Code Comment
Before: The code comment stated that the CS1540 error occurs "because x can only be accessed by classes derived from A" - but class B is derived from A, making this explanation incorrect.
After: Updated to accurately explain that "x can only be accessed through the derived class type, not through the base class type."
2. Incorrect Documentation Explanation
Before: The documentation claimed the error occurs "because it is made within the static method Main, and not an instance of class B" - but the static nature of the method is irrelevant to protected access rules.
After: Corrected to explain that the error occurs "because it accesses the protected member through a base class reference (
a
is of typeA
). Protected members can only be accessed through the derived class type or types derived from it."The Correct Rule
Protected members can only be accessed through references of the derived class type (or types derived from it), not through references of the base class type. This is why:
The compiler error message confirms this: "Cannot access protected member 'A.x' via a qualifier of type 'A'; the qualifier must be of type 'B' (or derived from it)"
Changes Made
csrefKeywordsModifiers.cs
protected.md
Fixes #17445.
💡 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