C# Reference ยท Keywords and Statements
Access modifiers: public, private, protected, internal
Reference for C# access modifiers: public, private, protected, internal, protected internal, private protected and file, with defaults and accessor rules.
Access modifiers decide which code may see a type or a member. They are the main tool for encapsulation: keep the details private, expose a small public surface, and you can change the inside later without breaking callers.
C# has six accessibility levels plus file for types: public, private, protected, internal, protected internal, private protected. The right level is the narrowest one that still lets the code that needs the member use it.
public class Account { private decimal balance; // only inside Account public decimal Balance => balance; // visible everywhere }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
public class A { } internal class B { } // default for top-level types file class C { } // visible only in this source file (C# 11) class Host { int a; // private (default for members) public int b; protected int c; internal int d; protected internal int e; // protected OR internal private protected int f; // protected AND internal public int Prop { get; private set; } }
| Modifier | Visible to |
|---|---|
public |
Any code that can see the containing type. |
private |
The containing type only (including nested types). Default for members of classes and structs. |
protected |
The containing type and types derived from it, in any assembly. |
internal |
Code in the same assembly. Default for top-level types. |
protected internal |
Same assembly or derived types anywhere. |
private protected |
Derived types and only inside the same assembly. |
file |
Types only: the same source file. |
Errors: using an inaccessible member is CS0122; making a member more accessible than its type (a public method returning an internal type) is CS0050 or CS0051; an accessor modifier that is not more restrictive than the property is CS0273.
Usage notes
- Defaults. Members default to
private; top-level types default tointernal; members of an interface default topublic; enum members are always public. - Nested types can be private or protected; top-level types can only be
public,internalorfile. - Accessor modifiers. One accessor of a property may be restricted:
public int X { get; private set; }. The other accessor keeps the property's level, and the restricted one must be strictly narrower. - Derived classes cannot widen or narrow an inherited member's accessibility when overriding it (CS0507), except that overriding
protected internalacross assemblies must useprotected. - protected is about the type of the instance. Code in a derived class can access a protected member only through an instance of its own type (or a further-derived one), not through a base-class reference to some other object.
- Interfaces: explicit interface implementations have no modifier; they are reachable only through the interface type.
- Not a security feature. Reflection can reach private members with enough trust. Treat the modifiers as design contracts.
- Assembly-wide access.
InternalsVisibleToon an assembly lets a named friend assembly, typically a test project, seeinternalmembers. - Public API size. Everything
publicis a promise you maintain. Start narrow and widen when needed; widening never breaks callers, narrowing does.
Related
- class
- sealed
- static
- init
- Tutorial: Access modifiers, Properties