A protected field in C# is a class-level variable declared with theDocumentation 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 access modifier, restricting its visibility strictly to the declaring class and any types derived from it. It establishes an inheritance-based encapsulation boundary, preventing direct access from external, non-derived types while exposing the internal state to subclasses.
Accessibility Rules
Theprotected modifier enforces compile-time access checks based on the inheritance hierarchy, regardless of assembly boundaries.
- Declaring Class: Full access.
- Derived Class (Same Assembly): Full access.
- Derived Class (Different Assembly): Full access.
- Non-Derived Class: Inaccessible (Compiler Error CS0122).
Cross-Instance Access Constraints (CS1540)
A critical mechanical rule of protected fields in C# is that a derived class can only access a protected field through an instance of its own type or a type derived from it. It cannot access the protected field through an instance of the base class or a different derived class.Structural Limitations
- Structs: Because C#
structtypes do not support inheritance, declaring aprotectedfield inside astructis syntactically invalid and results in Compiler Error CS0666. - Static Modifiers: A field can be declared
protected static. In this case, the field belongs to the type itself rather than an instance. Derived classes can access theprotected staticfield directly via the type name or implicitly, and the CS1540 instance-access rule does not apply. - Memory Layout: The
protectedkeyword is purely a compile-time visibility constraint. At runtime, a protected field occupies memory within the object instance exactly like aprivateorpublicfield.
Master C# with Deep Grasping Methodology!Learn More





