ADocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
protected property in C# is a class member that encapsulates data access using get and set accessors, while restricting its visibility exclusively to the declaring class and any classes derived from it. This access modifier enforces inheritance-based encapsulation, allowing state to cross assembly boundaries only through the inheritance hierarchy.
Syntax and Declaration
A protected property can be declared as an auto-implemented property or with an explicit backing field.Access Rules and Compilation Behavior
The C# compiler enforces strict rules regarding where aprotected property can be invoked. Access is evaluated at compile-time based on the static type of the reference.
- Declaring Class: Full access.
- Derived Class: Full access, but only through an instance of the derived class type (or a type derived from it).
- Non-Derived Class: Compiler Error CS0122 (Inaccessible due to its protection level).
The Instance Qualifier Rule
A critical mechanical constraint ofprotected members in C# is that a derived class cannot access a protected property on an instance declared as the base type. The reference must be qualified as the derived type.
Asymmetric Accessor Accessibility
C# allows property accessors to have different access modifiers. A common implementation is to expose the property publicly while restricting mutation to the inheritance hierarchy by applying theprotected modifier exclusively to the set accessor.
Compound Protected Modifiers
Theprotected keyword can be combined with other modifiers to alter the intersection of inheritance and assembly boundaries:
protected internal: The property is accessible to any class within the same assembly, OR any derived class in a different assembly. (Logical OR).private protected(C# 7.2+): The property is accessible only to derived classes that are declared within the same assembly. It prevents cross-assembly inheritance access. (Logical AND).
Static Protected Properties
When applied to astatic property, the protected modifier restricts access to the declaring class and derived classes, but the instance qualifier rule (CS1540) does not apply because static members are resolved against the type itself, not an instance.
Master C# with Deep Grasping Methodology!Learn More





