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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/csharp/language-reference/keywords/protected.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ A protected member of a base class is accessible in a derived class only if the

[!code-csharp[csrefKeywordsModifiers#11](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csrefKeywordsModifiers/CS/csrefKeywordsModifiers.cs#11)]

The statement `a.x = 10` generates an error because it is made within the static method Main, and not an instance of class B.
The statement `a.x = 10` generates an error 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.

Struct members cannot be protected because the struct cannot be inherited.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,8 @@ static void Main()
var a = new A();
var b = new B();

// Error CS1540, because x can only be accessed by
// classes derived from A.
// Error CS1540, because x can only be accessed through
// the derived class type, not through the base class type.
// a.x = 10;

// OK, because this class derives from A.
Expand Down
Loading