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.

The private protected access modifier restricts member visibility to the containing class and types derived from that class, strictly provided they reside within the same assembly. Introduced in C# 7.2, it acts as a logical AND operation between the internal (same assembly) and protected (derived types) modifiers, making it more restrictive than either modifier individually.
public class BaseClass
{
    private protected int _stateValue;
}

Access Mechanics

To access a private protected field, the consuming type must satisfy both of the following conditions simultaneously:
  1. Inheritance: The consuming type must inherit from the class declaring the field.
  2. Location: The consuming type must be compiled into the exact same assembly as the declaring class.
This contrasts directly with the protected internal modifier, which functions as a logical OR operation (accessible if derived or in the same assembly).

Compilation Boundaries

The following code block demonstrates the strict compilation boundaries enforced by the private protected modifier across different scopes and assemblies.
// =========================================
// Assembly A
// =========================================
public class BaseClass
{
    private protected int _coreField;
}

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

public class NonDerivedInSameAssembly
{
    public void AttemptMutation(BaseClass instance)
    {
        // ERROR CS0122: '_coreField' is inaccessible due to its protection level.
        // Fails condition: Not a derived type.
        instance._coreField = 42; 
    }
}

// =========================================
// Assembly B (References Assembly A)
// =========================================
public class DerivedInDifferentAssembly : BaseClass
{
    public void AttemptMutation()
    {
        // ERROR CS0122: '_coreField' is inaccessible due to its protection level.
        // Fails condition: Resides in Assembly B, not Assembly A.
        _coreField = 42; 
    }
}

CLR Implementation

At the Common Intermediate Language (CIL) level, the private protected modifier is represented by the famandassem accessibility attribute. This instructs the Common Language Runtime (CLR) to enforce the intersection of family (protected) and assembly (internal) visibility rules during Just-In-Time (JIT) compilation and execution.
Master C# with Deep Grasping Methodology!Learn More