Hi 👋 hope all is well! I like posting what I learn, whether accurate or inaccurate, in a fun way through movies or anime. Much like my previous post about SOLID principles, I thought to create a fun post about access modifiers in C#; but this time, with the help of AI 🤖.
C# Access Modifiers Explained Through Anime Characters
Access modifiers in C# control where and how code can be accessed, much like how anime characters have unique powers or story roles that define their reach and influence. Let’s break them down using iconic characters to make these concepts memorable... or weirder!
TL;DR
public
All Might: Everyone knows him.
private
Sasuke: Trusts no one outside himself.
protected
Gohan: Family (inheritance) only.
internal
Survey Corps: Assembly-wide access.
protected internal
Lelouch: Assembly OR derived classes.
private protected
Nezuko: Assembly AND family.
file
Ryuk: Trapped in one file.
1. public → All Might (My Hero Academia)
Power: Symbol of Peace, recognized by everyone.
Explanation: Just like All Might’s presence is known across all of Japan (and beyond), public members are accessible from any assembly. They have no restrictions, but their true "power" (accessibility) depends on their containing class. If the class itself is public, its public members shine globally.
Code Example:
public class HeroAcademy {
public string Motto = "Go Beyond, Plus Ultra!"; // Anyone can shout this!
}
2. private → Sasuke Uchiha (Naruto)
Power: Lone wolf, trusts only himself.
Explanation: Sasuke keeps his Sharingan and motives hidden from outsiders. Similarly, private members are accessible only within their own class or struct. Even derived classes can’t access them—like how Team 7 couldn’t peek into Sasuke’s darkest plans.
Code Example:
class UchihaClan {
private string SecretJutsu = "Chidori"; // Only Sasuke knows this.
}
3. protected → Gohan (Dragon Ball Z)
Power: Inherited Saiyan strength, shared with family.
Explanation: Gohan’s power comes from Goku (his base class) and is accessible to him and his descendants. Similarly, protected members are visible within their class and derived classes, but hidden from outsiders.
Code Example:
class Saiyan {
protected int PowerLevel = 9001; // Gohan can access this in the `HalfSaiyan` subclass.
}
4. internal → The Survey Corps (Attack on Titan)
Power: Defend Walls of their Assembly.
Explanation: The Survey Corps operate only within the walls of Paradis (their assembly). Internal members work the same way—accessible anywhere in the same assembly, but invisible outside it.
Code Example:
internal class ODMGear { // Only accessible within the "Paradis.Assembly" project.
internal int GasCapacity = 100;
}
5. protected internal → Lelouch vi Britannia (Code Geass)
Power: Geass commands work within his empire (assembly) or allies (derived classes).
Explanation: Lelouch’s influence extends to his own territory (assembly) or followers in other regions (derived classes in other assemblies). protected internal combines protected and internal: accessible in the same assembly or derived classes, even across assemblies.
Code Example:
protected internal void ZeroRequiem() {
// Can be called in the same assembly OR by derived classes elsewhere.
}
6. private protected → Nezuko Kamado (Demon Slayer)
Power: Protects her family (derived classes) but only within the Demon Slayer Corps (assembly).
Explanation: Nezuko’s loyalty is strictly to her brother Tanjiro (base class) and the Corps. Private protected is the strictest hybrid: accessible only in the same assembly and only by the same class or derived classes.
Code Example:
private protected void BloodArt() {
// Only accessible within the DemonSlayer.Assembly and Kamado family.
}
7. file → Ryuk (Death Note)
Power: Bound to a single Death Note (file).
Explanation: Ryuk can’t leave the Death Note’s owner (file). Introduced in C# 11, file restricts access to the same source file. It’s like a “localized” private for types like record or class.
Code Example:
file class Shinigami { // Only exists in this .cs file.
public string DeathNoteID = "Ryuk";
}
By framing access modifiers as anime characters, you can visualize their "scopes" of influence and restrictions. Now go code like a Pro Hero! 🚀
Hoped you liked this post, and as always please leave a comment, like, subscribe, disagree, agree etc 🙌 Hope you have a great start to the week, until next time!
Top comments (0)