Description
There seems to be some incorrect information in the first example.
// Error CS1540, because x can only be accessed by // classes derived from A. // a.x = 10;
B is derived from A, so that's not the reason why x cannot be accessed. B can only access x through a reference that is assignable to type B (meaning B or derived from B).
Another incorrect reason for the error is given under the example:
The statement
a.x = 10
generates an error because it is made within the static method Main, and not an instance of class B.
It makes no difference whether Main is a static method or an instance method. You can try removing the static modifier from Main and observe that the same error is still there. Again, the reason for the error is because B is trying to access x through an instance of A, and not because Main is static or because "x can only be accessed by classes derived from A."
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
- ID: 17ee29ae-d0ae-9f8e-545d-15b72901be7d
- Version Independent ID: ca4b8d67-d015-e47d-01bf-06be8c938e4d
- Content: protected keyword - C# Reference
- Content Source: docs/csharp/language-reference/keywords/protected.md
- Product: dotnet-csharp
- Technology: csharp-language-reference
- GitHub Login: @BillWagner
- Microsoft Alias: wiwagn
Metadata
Metadata
Assignees
Labels
Type
Projects
Status