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 private protected property in C# is a class member whose access is restricted to the containing class and types derived from that containing class, strictly provided that those derived types reside within the same assembly. It represents the logical intersection of internal and protected accessibility modifiers. At the Common Language Runtime (CLR) level, this access modifier maps to the FamilyAndAssembly visibility attribute.

Access Rules

The C# compiler enforces the following strict resolution rules for a private protected property:
  • Same Assembly + Derived Class: Accessible.
  • Same Assembly + Non-Derived Class: Inaccessible.
  • Different Assembly + Derived Class: Inaccessible.
  • Different Assembly + Non-Derived Class: Inaccessible.
This contrasts directly with protected internal (CLR FamilyOrAssembly), which acts as a logical union, granting access to derived classes regardless of their assembly, or any class within the same assembly.

Syntax and Visualization

// --- Assembly A 
public class BaseClass
{
    // Declaration of a private protected property
    private protected int InternalState { get; set; }
}

public class DerivedInSameAssembly : BaseClass
{
    public void MutateState()
    {
        // VALID: Type is derived from BaseClass AND resides in Assembly A
        this.InternalState = 42; 
    }
}

public class NonDerivedInSameAssembly
{
    public void AttemptMutation(BaseClass instance)
    {
        // COMPILER ERROR CS0122: 'BaseClass.InternalState' is inaccessible 
        // due to its protection level. (Not a derived class).
        // instance.InternalState = 42; 
    }
}
// --- Assembly B (References Assembly A) 
public class DerivedInDifferentAssembly : BaseClass
{
    public void AttemptMutation()
    {
        // COMPILER ERROR CS0122: 'BaseClass.InternalState' is inaccessible 
        // due to its protection level. (Derived class, but crosses assembly boundary).
        // this.InternalState = 42; 
    }
}

Property Accessor Asymmetry

The private protected modifier can be applied to the property declaration as a whole, or asymmetrically to individual accessors (get or set). When applied to an accessor, the private protected modifier must be strictly more restrictive than the property’s primary access modifier.
public class AccessorExample
{
    // The property getter is accessible to any type in the same assembly (internal).
    // The property setter is restricted to derived classes in the same assembly (private protected).
    internal string ConfigurationKey { get; private protected set; }
    
    // The property getter is accessible to derived classes anywhere (protected).
    // The property setter is restricted to derived classes in the same assembly (private protected).
    protected int Threshold { get; private protected set; }
}
Master C# with Deep Grasping Methodology!Learn More