Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix incorrect protected access modifier documentation and code comments #47038

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
Jul 2, 2025

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jun 30, 2025

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 type A). 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:

class B : A 
{
    static void Main() 
    {
        var a = new A();
        var b = new B();
        
        a.x = 10; // ❌ CS1540 - accessing through base class reference (A)
        b.x = 10; // ✅ OK - accessing through derived class reference (B)
    }
}

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

  • Updated code comment in csrefKeywordsModifiers.cs
  • Updated explanation text in protected.md
  • Both changes are minimal and surgical, preserving all existing functionality

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

📄 File 🔗 Preview link
docs/csharp/language-reference/keywords/protected.md docs/csharp/language-reference/keywords/protected

@Copilot Copilot AI changed the title [WIP] Wrong information in first protected example Fix incorrect protected access modifier documentation and code comments Jun 30, 2025
@Copilot Copilot AI requested a review from BillWagner June 30, 2025 19:52
Copilot finished work on behalf of BillWagner June 30, 2025 19:52
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.

I'll mark it ready for review.

@BillWagner BillWagner marked this pull request as ready for review July 1, 2025 17:46
@BillWagner BillWagner requested review from IEvangelist and a team as code owners July 1, 2025 17:46
@BillWagner
Copy link
Member

@IEvangelist Can you review?

@BillWagner BillWagner enabled auto-merge (squash) July 1, 2025 17:46
@BillWagner BillWagner merged commit 60df0ef into main Jul 2, 2025
22 checks passed
@BillWagner BillWagner deleted the copilot/fix-17445 branch July 2, 2025 14:12
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.

Wrong information in first protected example
3 participants