Skip to content

Latest commit

 

History

History
60 lines (46 loc) · 2.85 KB

File metadata and controls

60 lines (46 loc) · 2.85 KB
title protected (C# Reference)
ms.date 07/20/2015
ms.prod .net
ms.technology
devlang-csharp
ms.topic article
f1_keywords
protected
protected_CSharpKeyword
helpviewer_keywords
protected keyword [C#]
ms.assetid 05ce3794-6675-4025-bddb-eaaa0ec22892
caps.latest.revision 20
author BillWagner
ms.author wiwagn

protected (C# Reference)

The protected keyword is a member access modifier.

This page covers protected access. The protected keyword is also part of the protected internal and private protected access 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.

Example

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.

Example

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.

C# Language Specification

[!INCLUDECSharplangspec]

See Also

C# Reference
C# Programming Guide
C# Keywords
Access Modifiers
Accessibility Levels
Modifiers
public
private
internal
Security concerns for internal virtual keywords