> ## 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# Protected Method

A `protected` method in C# is a class member encapsulated with the `protected` access modifier, restricting its visibility strictly to the declaring class and any types derived from that class, regardless of assembly boundaries. It establishes an inheritance-based accessibility contract, hiding the implementation from the public API while exposing it to the inheritance hierarchy.

## Syntax and Declaration

The `protected` keyword precedes the return type in the method signature. It can be applied to both instance and static methods.

```csharp theme={"dark"}
public class BaseClass
{
    protected void ExecuteCoreLogic()
    {
        // Method body
    }

    protected static void ExecuteStaticLogic()
    {
        // Static method body
    }
}
```

## Accessibility Rules and Compiler Behavior

The C# compiler enforces strict rules regarding where and how a `protected` method can be invoked. Attempting to access a protected method outside its permitted scope results in compiler error **CS0122** (`'member' is inaccessible due to its protection level`).

* **Declaring Class:** Fully accessible.
* **Derived Class (Same Assembly):** Accessible.
* **Derived Class (Different Assembly):** Accessible.
* **Non-Derived Class (Any Assembly):** Inaccessible.

### The Instance Access Restriction (CS1540)

A critical rule of the `protected` modifier is that within a derived class, a protected base instance method can only be accessed through an instance of the derived class type (or a type further derived from it). It cannot be accessed through an instance of the base class type.

This prevents a derived class from bypassing encapsulation to access the protected members of other, unrelated derived classes that share the same base.

```csharp theme={"dark"}
public class BaseClass
{
    protected void Initialize() { }
}

public class DerivedClass : BaseClass
{
    public void TestAccess()
    {
        // Valid: Implicitly accessing via 'this' (DerivedClass instance)
        Initialize(); 

        // Valid: Accessing via an explicit DerivedClass instance
        DerivedClass derived = new DerivedClass();
        derived.Initialize();

        // Invalid: Compiler Error CS1540
        // Cannot access protected member via a qualifier of type 'BaseClass'
        BaseClass baseInstance = new BaseClass();
        baseInstance.Initialize(); 
    }
}
```

*Note: This restriction applies only to instance methods. `protected static` methods can be accessed directly via the base class name from within the derived class.*

## Compound Access Modifiers

The `protected` keyword can be combined with other access modifiers to alter the Common Intermediate Language (CIL) visibility attributes, creating union or intersection accessibility sets:

* **`protected internal`**: A union. The method is accessible to any type in the same assembly (`internal`) **OR** any derived type in any assembly (`protected`).
* **`private protected`**: An intersection (introduced in C# 7.2). The method is accessible only to derived types that are **ALSO** located within the same assembly. It is strictly inaccessible across assembly boundaries.

## Polymorphic Modifiers

`protected` methods fully support C#'s polymorphic modifiers. They can be marked as `virtual`, `abstract`, or `override`. When overriding a protected method, the derived class must maintain the exact `protected` access level; it cannot widen the visibility to `public` or narrow it to `private`.

```csharp theme={"dark"}
public abstract class AbstractBase
{
    // Requires implementation by derived classes
    protected abstract void Compute();
    
    // Permits overriding by derived classes
    protected virtual void Format() { }
}

public class ConcreteDerived : AbstractBase
{
    // Must retain the 'protected' modifier
    protected override void Compute() { }
    
    // Must retain the 'protected' modifier
    protected override void Format() { }
}
```

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