Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A 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.
public class BaseClass
{
    // Auto-implemented protected property
    protected int StateCode { get; set; }

    // Protected property with an explicit backing field
    private string _systemHash;
    protected string SystemHash
    {
        get => _systemHash;
        set => _systemHash = value;
    }
}

Access Rules and Compilation Behavior

The C# compiler enforces strict rules regarding where a protected property can be invoked. Access is evaluated at compile-time based on the static type of the reference.
  1. Declaring Class: Full access.
  2. Derived Class: Full access, but only through an instance of the derived class type (or a type derived from it).
  3. Non-Derived Class: Compiler Error CS0122 (Inaccessible due to its protection level).

The Instance Qualifier Rule

A critical mechanical constraint of protected 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.
public class DerivedClass : BaseClass
{
    public void MutateState(BaseClass baseInstance, DerivedClass derivedInstance)
    {
        // Valid: Accessing via the implicit 'this' reference
        this.StateCode = 100;

        // Valid: Accessing via an instance of the derived type
        derivedInstance.StateCode = 200;

        // INVALID: Compiler Error CS1540
        // Cannot access protected member via a qualifier of type 'BaseClass'
        // baseInstance.StateCode = 300; 
    }
}

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 the protected modifier exclusively to the set accessor.
public class ConfigurationNode
{
    // The property is public, but the setter is protected
    public string NodeId { get; protected set; }
}
Note: The accessibility modifier on the individual accessor must be more restrictive than the modifier on the property itself.

Compound Protected Modifiers

The protected 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).
public class AdvancedBase
{
    protected internal int CrossAssemblyOrLocal { get; set; }
    private protected int StrictlyLocalDerived { get; set; }
}

Static Protected Properties

When applied to a static 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.
public class BaseStatic
{
    protected static int GlobalCounter { get; set; }
}

public class DerivedStatic : BaseStatic
{
    public static void Increment()
    {
        // Valid: Accessed via the derived type context
        GlobalCounter++; 
    }
}
Master C# with Deep Grasping Methodology!Learn More