| title | protected (C# Reference) | ||
|---|---|---|---|
| ms.date | 07/20/2015 | ||
| ms.prod | .net | ||
| ms.technology |
|
||
| ms.topic | article | ||
| f1_keywords |
|
||
| helpviewer_keywords |
|
||
| ms.assetid | 05ce3794-6675-4025-bddb-eaaa0ec22892 | ||
| caps.latest.revision | 20 | ||
| author | BillWagner | ||
| ms.author | wiwagn |
The protected keyword is a member access modifier.
This page covers
protectedaccess. Theprotectedkeyword is also part of theprotected internalandprivate protectedaccess modifiers.
A protected member is accessible within its class and by derived class instances.
For a comparison of protected with the other access modifiers, see Accessibility Levels.
A protected member of a base class is accessible in a derived class only if the access occurs through the derived class type. For example, consider the following code segment:
[!code-csharpcsrefKeywordsModifiers#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.
Struct members cannot be protected because the struct cannot be inherited.
In this example, the class DerivedPoint is derived from Point. Therefore, you can access the protected members of the base class directly from the derived class.
[!code-csharpcsrefKeywordsModifiers#12]
If you change the access levels of x and y to private, the compiler will issue the error messages:
'Point.y' is inaccessible due to its protection level.
'Point.x' is inaccessible due to its protection level.
[!INCLUDECSharplangspec]
C# Reference
C# Programming Guide
C# Keywords
Access Modifiers
Accessibility Levels
Modifiers
public
private
internal
Security concerns for internal virtual keywords