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.

A protected internal field is a class-level variable declared with a compound access modifier that grants the union of both protected and internal accessibility. It represents a logical OR operation between the two modifiers. The field is accessible to any code within the same assembly (the internal aspect) and to any derived class, regardless of whether that derived class resides in the same assembly or a different one (the protected aspect).

Syntax

The modifiers can be written in either order, though protected internal is the standard C# convention.
public class BaseClass
{
    protected internal int accessCount;
    internal protected string statusFlag; // Valid, but less conventional
}

Accessibility Matrix

The compiler enforces the following access rules for a protected internal field:
ContextAccessible?Reason
Same ClassYesStandard member access.
Same Assembly, Derived ClassYesSatisfies both internal and protected.
Same Assembly, Non-Derived ClassYesSatisfies internal.
Different Assembly, Derived ClassYesSatisfies protected.
Different Assembly, Non-Derived ClassNoSatisfies neither modifier.

Mechanical Demonstration

The following code blocks illustrate the compiler’s behavior when resolving access to a protected internal field across assembly boundaries. Assembly A (The defining assembly)
namespace AssemblyA
{
    public class CoreSystem
    {
        protected internal string systemState = "Initialized";
    }

    public class LocalMonitor
    {
        public void ReadState()
        {
            var core = new CoreSystem();
            // VALID: Accessed by a non-derived class within the SAME assembly.
            Console.WriteLine(core.systemState); 
        }
    }
}
Assembly B (An external assembly referencing Assembly A)
using AssemblyA;

namespace AssemblyB
{
    public class ExtendedSystem : CoreSystem
    {
        public void CheckState()
        {
            // VALID: Accessed by a derived class in a DIFFERENT assembly.
            Console.WriteLine(this.systemState); 
            
            var core = new CoreSystem();
            // INVALID: Compiler Error CS1540. 
            // Across assemblies, 'protected' rules apply. You cannot access 
            // the field via an instance of the base type, only via an 
            // instance of the derived type.
            // Console.WriteLine(core.systemState); 
        }
    }

    public class ExternalMonitor
    {
        public void AttemptRead()
        {
            var core = new CoreSystem();
            // INVALID: Compiler Error CS0122. 
            // Inaccessible due to its protection level (neither internal nor protected).
            // Console.WriteLine(core.systemState); 
        }
    }
}

Technical Distinctions

  • Instance Qualification Rule: When a protected internal field is accessed from a derived class in a different assembly, it is subject to standard protected access rules. It can only be accessed through an instance of the derived class type (or a type derived from it), not directly through an instance of the base class type.
  • Contrast with private protected: Do not confuse protected internal (Logical OR) with private protected (Logical AND). A private protected field is accessible only to derived classes that reside within the same assembly.
Master C# with Deep Grasping Methodology!Learn More