> ## 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.

# C# Private Protected Method

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 Location    | Relationship to Base Class          | Access Granted?                                     |
| :----------------- | :---------------------------------- | :-------------------------------------------------- |
| Same Assembly      | Declaring class or nested type      | **Yes**                                             |
| Same Assembly      | Derived class                       | **Yes**                                             |
| Same Assembly      | Unrelated (Not derived, not nested) | No (Compiler Error)                                 |
| Different Assembly | Derived class                       | No\* (**Yes** if `[InternalsVisibleTo]` is applied) |
| Different Assembly | Unrelated                           | No (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.

```csharp theme={"dark"}
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).

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C# Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
