Accessibility or Visibility Modifiers in C - Sharp
Accessibility or Visibility Modifiers in C - Sharp
Keyword Accessibility
Public - Entire Program: Yes
- Containing Class: Yes
- Current Assembly: Yes
- Derived types: Yes
Protected - Entire Program: No
- Containing Class: Yes
- Current Assembly: No
- Derived types: Yes
Internal - Entire Program: No
- Containing Class: Yes
- Current Assembly: Yes
- Derived types: No
Protected - Entire Program: No
Internal
- Containing Class: Yes
- Current Assembly: Yes
- Derived types: Yes
Private - Entire Program: No
- Containing Class: Yes
- Current Assembly: No
- Derived types: No
Private - Entire Program: No
Protected
- Containing Class: Yes
- Current Assembly: No
- Derived types: No (unless same assembly and derived)
2. Vocabulary Breakdown
o Containing Class (Yes): Of course the class itself can use its
own public members.
o Current Assembly (Yes): Code in the same compiled DLL sees
it.
3. Why Public?
4. Edge Cases
5. Analogies
2. Vocabulary Breakdown
3. Why Protected?
4. Underlying Assumptions
6. Analogy
o A secret family book: only members of the family tree (the class
and subclasses) can read it. A cousin (unrelated class) in the
same town (assembly) can’t peek.
Section 3 — Internal
1. Plain-English “Internal” means “any code in the same assembly can
see me, but outsiders (other assemblies) cannot.” In mansion terms,
this is like an internal-only staff elevator—it’s open to employees who
wear the building badge (part of the same assembly), but guests can’t
access it.
2. Vocabulary Breakdown
3. Why Internal?
4. Edge Cases
5. Analogy
2. Vocabulary Breakdown
o Containing Class (Yes): As always, the class itself can see its
own members.
4. Deeper Reasoning
o I pause: Why “OR” and not “AND”? Because “AND” would be too
restrictive—“protected AND internal” means the subclass must
also live in the same assembly, which severely diminishes
extensibility across assemblies. Luckily, for that exact
intersection, C# provides “private protected”.
5. Analogy
Section 5 — Private
1. Plain-English “Private” means “only the containing class (and its
nested types) can see me; everyone else—assembly mates and
subclasses alike—cannot.” In mansion terms, this is a hidden vault
sealed behind a door that only the room’s owner (the containing class)
can open.
2. Vocabulary Breakdown
3. Why Private?
4. Edge Considerations
5. Analogy
2. Vocabulary Breakdown
Thus:
4. Concrete Example
A method ComputeInterest().
A method Deposit().
A method Withdraw().
Both compile to the same IL metadata. So, convention matters more than
function.
o Only the class itself and any subclass (in any assembly) sees it.
No one else, even if in the same assembly.
Internal
o Any code in the same assembly sees it. Even non-derived classes
see it. But no one outside the assembly sees it.
Protected Internal (“protected OR internal”)
o Only the containing class (and its nested types) sees it. No
siblings, no cousins, no outsiders.
Private Protected (“protected AND internal”)