skip to content

In C#, how do protected internal and private protected differ, and which code in another assembly can reach each member?

level: middleimportance: should knowfreq 52%

answer

  1. one is a union, one an intersection
  2. assembly OR derived vs assembly AND derived
  3. the qualifier's static type matters
  4. C# 7.2 added the stricter one

basics

~20 s

protected internal means any code in the same assembly or a derived class anywhere. private protected (C# 7.2) means only derived classes in the same assembly, so another assembly's code never reaches it unless that assembly is a friend.

solid answer

~40 s

`protected internal` is the union of its two words: every type in the declaring assembly can use the member, and so can a derived class in any other assembly. `private protected`, added in C# 7.2, is the intersection: the user must be a derived class *and* live in the declaring assembly. From another assembly, a derived class can reach a `protected internal` member only through `this` or a variable of its own type (CS1540 otherwise) and cannot reach a `private protected` one at all (CS0122). Non-derived code in another assembly reaches neither. If the declaring assembly names the other one with `InternalsVisibleTo`, it counts as the same assembly for both.

code

csharp · 20 lines
csharp
var card = new CardFee();
Console.WriteLine(card.Describe(new CardFee()));
Console.WriteLine(new FeeAudit().Read(card));

public class Fee
{
    private protected int surcharge = 2;   // derived classes in this assembly only
    protected internal int discount = 5;   // this assembly, or derived classes anywhere
}

public class CardFee : Fee
{
    public string Describe(CardFee other) => $"{surcharge} {other.surcharge} {discount}";
}

class FeeAudit                             // not derived, same assembly
{
    public int Read(Fee fee) => fee.discount;          // allowed: the internal half
    // public int Peek(Fee fee) => fee.surcharge;      // CS0122: not derived
}

go deeper

for a junior

Know that C# has six levels and that the two compound ones exist. protected internal is the wider one, private protected the narrower one.

for a middle

Explain union versus intersection and fill in the who-can-reach table, including the CS1540 qualifier rule for derived classes in another assembly.

for a senior

Use private protected to keep base-class hooks out of a public library's compatibility surface, and know that InternalsVisibleTo makes a friend assembly count as the same assembly.

for a principal

Treat every protected-family member of a public class as API that external subclasses depend on; decide per hook whether the extensibility is worth that long-term promise.

## Two compound modifiers, opposite logic C# has four access keywords and six accessibility levels, because two levels are **combinations**: - **`protected internal`** reads as *protected OR internal*. A member is accessible if the accessing code is in the declaring assembly, **or** if it is in a class derived from the declaring class, wherever that class lives. - **`private protected`** reads as *protected AND internal* (the `private` narrows the protected part to the assembly). A member is accessible only if the accessing code is in a derived class **and** in the declaring assembly. The compiler accepts it from C# 7.2 on. Word order does not matter for the meaning; the pair, not the order, picks the level. ## Who can reach what Take a library assembly with `public class Fee` declaring `private protected int surcharge` and `protected internal int discount`, and an application assembly that references it. | Accessing code | `protected` | `protected internal` | `private protected` | |---|---|---|---| | Non-derived class, same assembly | no | **yes** | no | | Derived class, same assembly | yes | yes | **yes** | | Derived class, other assembly | yes | yes | **no** (CS0122) | | Non-derived class, other assembly | no | **no** (CS0122) | no | So `private protected` is the tightest of the three protected levels and `protected internal` the loosest. Pick `private protected` for a hook that only your own subclasses in your own library should call; pick `protected internal` when your assembly's helpers need the member and external subclasses should get it too. ## The qualifier rule still applies to the protected half Protected access across a hierarchy has a second condition: when a derived class reaches the member through an expression other than `this` or `base`, that expression's **static type** must be the derived class or a subclass of it. Reading `other.discount` where `other` is typed as the base `Fee` from a derived class in another assembly is CS1540: "Cannot access protected member 'Fee.discount' via a qualifier of type 'Fee'; the qualifier must be of type 'CardFee' (or derived from it)". The same CS1540 appears for a `private protected` member read through a base-typed variable inside the declaring assembly. Inside the declaring assembly the `internal` half of `protected internal` makes the qualifier rule irrelevant: any code there can use it through any reference. ## Friend assemblies count as "the same assembly" `[assembly: InternalsVisibleTo("Billing.Core.Tests")]` makes the named assembly a **friend**. For access checks the friend is treated like the declaring assembly, so: - any code in the friend can use `protected internal` members through any reference; - a derived class in the friend can use `private protected` members. Without the attribute, both lines of the table's "other assembly" rows hold. ## Overrides and structs - Overriding a `protected internal` virtual member from **another** assembly must be declared `protected`; writing `protected internal override` there is CS0507 ("cannot change access modifiers when overriding 'protected internal' inherited member"). Inside the declaring assembly the override keeps `protected internal`. - Structs cannot be inherited, so they cannot declare members at any `protected` level; a `protected` field in a struct is CS0666. ## What the build actually reports With `Fee` in a `Billing.Core` library and two consumers, the SDK gives these results: - a non-derived class in a referencing application reads `fee.discount` (`protected internal`): CS0122, because neither half of the union applies; - a derived class in that application reads `surcharge` (`private protected`) through `this`: CS0122, because it fails the same-assembly half; - the same derived class reads `discount` through `this` or a variable of its own type: compiles; - it reads `other.discount` with `other` typed as `Fee`: CS1540; - a test project named in the library's `InternalsVisibleTo` reads `fee.discount` from a non-derived class, and reads `surcharge` from a class derived from `Fee`: both compile and run. ## How to answer under pressure 1. Translate each modifier into words: OR for `protected internal`, AND for `private protected`. 2. Ask two questions about the accessing code: is it in the declaring assembly (or a friend), and is it in a derived class? 3. For a derived class outside the assembly, check the qualifier's static type.

  • When would you choose private protected over protected for a base-class hook?
    When the base class is `public` so others can use it, but only your own subclasses inside the library should call or override the hook. `protected` would put the member on every external subclass's surface and make it part of your compatibility promise; `private protected` keeps it out of the public API while your internal hierarchy shares it.
  • How must an override of a protected internal virtual method be declared in another assembly?
    As `protected override`. The overriding assembly cannot see the base's internal half, so the compiler expects only `protected`; writing `protected internal override` there is CS0507. Inside the declaring assembly the override must repeat `protected internal`.

  • Says private protected lets derived classes in any assembly access the member.
  • Thinks protected internal means only derived classes inside the assembly.
  • Believes the order of the two keywords changes the meaning.
  • Assumes a derived class can read a protected member through any base-typed variable.
  • Thinks a struct can declare a protected internal member.