Skip to content

1.4 — Access Modifiers

← Roadmap · Section: 1. C# Language Fundamentals Prev: 1.3 Strings · Next: 1.5 static

Markers: — · Confidence: 🔴 · Last reviewed: —


TL;DR

  • There are six. The two double-word ones catch people out: protected internal means "either one" and private protected means "both together".
  • If you write nothing: class members are private, and top-level classes are internal.
  • internal is the one people forget. It lets you hide things from other projects without hiding them from your own code.

The concept

Access modifiers answer one question: who is allowed to see this?

Before the list, two words you need:

  • Assembly — one compiled project. One .dll or .exe file. If your solution has MyApp.Web and MyApp.Core as separate projects, those are two assemblies.
  • Derived type — a class that inherits from your class.

Interviewers mostly ask this to check you know the two double-word modifiers and the defaults, because those are the parts nobody looks up.


How it works

Modifier Who can see it
public Everyone, everywhere
private Only code inside the same class
protected The same class, and any class that inherits from it (even in another project)
internal Any code in the same project
protected internal Same project, OR any inheriting class anywhere
private protected Only inheriting classes that are also in the same project

The two confusing ones

Read these slowly, because most people guess them backwards.

protected internal = protected OR internal. You get in if you are in the same project, or if you inherit from the class. Either one is enough. This makes it more open than protected alone or internal alone.

private protected = protected AND internal. You need both: you must inherit from the class and be in the same project. This makes it more closed than either one alone.

A memory trick that survives interview nerves:

protected internal opens the door wider. private protected narrows it.

The word private in the second one is your hint that it is the restrictive one.

What happens if you write nothing

What you are declaring Default if you leave the modifier off
A class, struct, record, interface or enum at the top level internal
A field, method or property inside a class private
A member of an interface public (and before C# 8 it could not be anything else)
A member of an enum public

So this:

class Foo
{
    int x;
}

is exactly the same as this:

internal class Foo      // NOT public!
{
    private int x;
}

That first one catches people out when building a class library. Your class compiles fine, but nobody outside your project can use it, and the error message ("inaccessible due to its protection level") does not obviously say "you forgot public".

You cannot leak a hidden type through a visible one

The compiler stops you from exposing something more private than the thing exposing it:

internal class Config { }

public class Service
{
    public Config GetConfig() => new();
    // ERROR!
    // The method is public, so anyone can call it.
    // But it returns Config, which is internal — outsiders
    // cannot even name that type. The compiler blocks this.
}

Testing internal classes

internal is not a locked door. You can open it to specific projects, which is the normal way to unit-test internal classes:

<!-- In your .csproj file -->
<ItemGroup>
  <InternalsVisibleTo Include="MyApp.Tests" />
</ItemGroup>

Now the test project can see internal types, but nobody else can. Knowing this is a small but real credibility signal in an interview, because the common bad answer is "I make things public so I can test them" — which permanently widens your API just for testing convenience.


Code

public class Account
{
    // Nobody outside this class needs to know how we store the balance
    private decimal _balance;

    // Subclasses need this, and they might live in another project
    protected string AccountNumber { get; init; } = "";

    // Other classes in THIS project use it. Outside consumers do not.
    internal Guid InternalId { get; init; }

    // The actual public contract
    public decimal Balance => _balance;

    // Only subclasses, and only ones inside this project
    private protected virtual void Audit() { }

    // Anyone in this project, OR any subclass anywhere
    protected internal virtual void Reset() { }
}

One extra thing worth knowing — a property can have different access on get and set:

public string Name { get; private set; }
// Anyone can READ Name.
// Only code inside this class can CHANGE it.

This is a very common and useful pattern, and people often forget it exists.


Interview questions

Q: What is the difference between protected internal and private protected?

protected internal means "either one is enough" — you can reach it from anywhere in the same project, or from a class that inherits from it in any project. That makes it more open than either part on its own. private protected means "you need both" — only inheriting classes that are also in the same project can reach it, which makes it more restrictive than either part. private protected was added in C# 7.2 because there was no way to express that combination before.

Q: What is the default accessibility of a class and of a class member?

A top-level class defaults to internal, and a member inside a class defaults to private. The class default surprises people writing a library, because their type compiles fine but consumers cannot see it. The member default is usually what you want anyway, so writing private on fields is a style choice rather than a behaviour change.

Q: How do you unit-test an internal class without making it public?

You add InternalsVisibleTo pointing at the test project, either as an assembly attribute or as an item in the .csproj. That lets the tests see internal types while keeping them out of the public API. The alternative — making the class public just so the tests can reach it — means you have permanently committed to supporting that type for external consumers, which is a real cost for a testing convenience.


Traps ⚠️

  • Getting the two compound modifiers backwards. Most people guess that protected internal is restrictive because it has two limiting words in it. It is the opposite — it is the permissive one.
  • Forgetting classes default to internal. Your library type is invisible to consumers and the error message does not point at the missing public.
  • internal is not security. Reflection can reach it, and InternalsVisibleTo opens it. It is a tool for keeping your API surface small, not for protecting secrets.
  • Forgetting you can set accessibility per accessor. { get; private set; } is often exactly what you want and saves writing a backing field by hand.


Sources