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 in C# restricts member visibility to the declaring class, types nested within the declaring class, and derived classes that reside within the same assembly (or a designated friend assembly). It functions as a logical AND operation between the internal (assembly-level) and protected (inheritance-level) modifiers. To access a private protected method from outside the declaring class or its nested types, the calling type must inherit from the containing class and be compiled into the same assembly, unless the defining assembly explicitly grants access via the [InternalsVisibleTo] attribute. This modifier was introduced in C# 7.2 to provide stricter encapsulation boundaries compared to protected internal, which acts as a logical OR (accessible to any type in the same assembly, or any derived type in any assembly).

Access Rules Matrix

Caller LocationRelationship to Base ClassAccess Granted?
Same AssemblyDeclaring class or nested typeYes
Same AssemblyDerived classYes
Same AssemblyUnrelated (Not derived, not nested)No (Compiler Error)
Different AssemblyDerived classNo* (Yes if [InternalsVisibleTo] is applied)
Different AssemblyUnrelatedNo (Compiler Error)

Syntax and Compilation Behavior

The following code block demonstrates the mechanical enforcement of the private protected modifier across assembly boundaries, nested types, and inheritance hierarchies.
using System;
using System.Runtime.CompilerServices;

// Optional: Grants internal access to Assembly B
// [assembly: InternalsVisibleTo("AssemblyB")]

// =========================================
// Assembly A (e.g., CoreLibrary.dll)
// =========================================
public class BaseClass
{
    // Declaring the private protected method
    private protected void ExecuteCoreLogic()
    {
        Console.WriteLine("Executing logic...");
    }

    public void CallFromDeclaringType()
    {
        // VALID: Accessed directly from within the declaring class.
        ExecuteCoreLogic();
    }

    public class NestedClass
    {
        public void CallFromNested(BaseClass instance)
        {
            // VALID: Accessed from a type nested within the declaring class.
            instance.ExecuteCoreLogic();
        }
    }
}

public class DerivedInSameAssembly : BaseClass
{
    public void TestAccess()
    {
        // VALID: Caller is a derived class AND resides in Assembly A.
        ExecuteCoreLogic(); 
    }
}

public class NonDerivedInSameAssembly
{
    public void TestAccess(BaseClass instance)
    {
        // ERROR CS0122: 'BaseClass.ExecuteCoreLogic()' is inaccessible 
        // due to its protection level.
        // Reason: Resides in Assembly A, but does NOT inherit from BaseClass.
        // instance.ExecuteCoreLogic(); 
    }
}

// =========================================
// Assembly B (e.g., ExternalApp.exe)
// Requires project reference to Assembly A
// =========================================
public class DerivedInDifferentAssembly : BaseClass
{
    public void TestAccess()
    {
        // ERROR CS0122: 'BaseClass.ExecuteCoreLogic()' is inaccessible 
        // due to its protection level.
        // Reason: Inherits from BaseClass, but resides in Assembly B.
        // (NOTE: This WOULD compile successfully if Assembly A declared 
        // [assembly: InternalsVisibleTo("AssemblyB")])
        // ExecuteCoreLogic(); 
    }
}

Technical Constraints

  • Language Version: Requires C# 7.2 or later.
  • Valid Targets: Can be applied to methods, properties, fields, events, and nested types within a class or record.
  • Invalid Targets: Cannot be applied to members of a struct (as structs do not support inheritance) or members of an interface (prior to C# 8.0 default interface methods). It also cannot be applied to top-level types (classes or structs declared directly within a namespace).
Master C# with Deep Grasping Methodology!Learn More