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

A protected field in C# is a class-level variable declared with the `protected` access modifier, restricting its visibility strictly to the declaring class and any types derived from it. It establishes an inheritance-based encapsulation boundary, preventing direct access from external, non-derived types while exposing the internal state to subclasses.

```csharp theme={"dark"}
public class BaseClass
{
    protected int _protectedField;
}
```

## Accessibility Rules

The `protected` modifier enforces compile-time access checks based on the inheritance hierarchy, regardless of assembly boundaries.

* **Declaring Class:** Full access.
* **Derived Class (Same Assembly):** Full access.
* **Derived Class (Different Assembly):** Full access.
* **Non-Derived Class:** Inaccessible (Compiler Error CS0122).

## Cross-Instance Access Constraints (CS1540)

A critical mechanical rule of protected fields in C# is that a derived class can only access a protected field through an instance of its *own* type or a type derived from it. It cannot access the protected field through an instance of the base class or a different derived class.

```csharp theme={"dark"}
public class BaseClass
{
    protected int _identifier;
}

public class DerivedClass : BaseClass
{
    public void EvaluateAccess()
    {
        // Valid: Implicit access to 'this._identifier'
        _identifier = 1; 

        // Valid: Access via an instance of the exact derived type
        DerivedClass derivedInstance = new DerivedClass();
        derivedInstance._identifier = 2;

        // Invalid: Access via an instance of the base type
        BaseClass baseInstance = new BaseClass();
        // baseInstance._identifier = 3; // Compiler Error CS1540
    }
}

public class SiblingClass : BaseClass
{
    public void EvaluateSiblingAccess(DerivedClass sibling)
    {
        // Invalid: Access via an instance of a different derived type
        // sibling._identifier = 4; // Compiler Error CS1540
    }
}
```

## Structural Limitations

* **Structs:** Because C# `struct` types do not support inheritance, declaring a `protected` field inside a `struct` is syntactically invalid and results in Compiler Error CS0666.
* **Static Modifiers:** A field can be declared `protected static`. In this case, the field belongs to the type itself rather than an instance. Derived classes can access the `protected static` field directly via the type name or implicitly, and the CS1540 instance-access rule does not apply.
* **Memory Layout:** The `protected` keyword is purely a compile-time visibility constraint. At runtime, a protected field occupies memory within the object instance exactly like a `private` or `public` field.

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