Skip to main content
A protected field in C# is a class-level variable declared with the 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

The protected 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# struct types do not support inheritance, declaring a protected field inside a struct is 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 the protected static field directly via the type name or implicitly, and the CS1540 instance-access rule does not apply.
  • Memory Layout: The protected keyword is purely a compile-time visibility constraint. At runtime, a protected field occupies memory within the object instance exactly like a private or public field.
Tired of Poor C# Skills? Fix That With Deep Grasping!Learn More