LearnThatStack Ace your next interview
C# · question
Question 7 of 112

Explain access modifiers in C#.

beginner
← All C# questions
Re-explain

Access modifiers control which code can use a type or member.

  • public is accessible from anywhere.
  • private is accessible only inside the containing class.
  • protected is accessible in the class and its derived classes.
  • internal is accessible anywhere in the same assembly.
  • protected internal is accessible in the same assembly, or from derived classes in any assembly.
  • private protected is accessible only from derived classes in the same assembly.

Members of a class default to private, and top-level types default to internal. Pick the most restrictive one that works.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

  • Can one instance of a class read another instance's private fields? Yes. Private is scoped to the type, not the object, so a method can read private fields of any instance of the same class.
  • Inside a derived class, can you call a protected instance member through a base-class variable? No. The access must go through the derived type or one of its subclasses, so a reference typed as the base class fails to compile.
  • How would unit tests reach internal members without making them public? Name InternalsVisibleTo on the assembly under test, and its cost. The tests now depend on internal details that were meant to change freely.
  • Why expose a public property instead of a public field? A property lets you add validation or change storage later, and the setter can take a narrower modifier, like private set.
  • Do access modifiers stop other code from reading your private data? No. Reflection can read private members. Modifiers are a compile-time design boundary, not a security control.

What you can say

  1. Access modifiers decide which code is allowed to use a type or a member, so they're how a class keeps its internals hidden.
  2. The basic four are public for anyone, private for the containing class only, protected for the class and its subclasses, and internal for anything in the same assembly.
  3. Protected internal is the looser combination, same assembly or any derived class. Private protected is the tighter one, derived classes inside the same assembly only.
  4. If you don't write a modifier, class members are private and top-level types are internal.
  5. I start with the most restrictive modifier that works, because opening access later is easy, while narrowing it breaks code that already depends on it.

Weak answers to avoid

  • Mixes up protected internal and private protected They're opposites. Protected internal is same assembly or any derived class. Private protected is derived classes in the same assembly only. Say which one is wider.
  • Says internal means same namespace Internal is scoped to the assembly, the compiled DLL. Namespaces only organize names and never restrict access.
  • Gets the defaults wrong Class members default to private and top-level types to internal. Guessing public suggests you've never checked what you get when you leave the modifier off.
  • Recites the list with no design reason Reciting six definitions only shows recall. Say you start with the narrowest access, since every public member becomes a contract other code depends on.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the C# cheatsheet.

← Back to all C# questions
Pro · $10/mo

98 of 112 C# answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime