DEFENSE PROTOCOL | REFERENCE ARTICLE | PROTECTED KEYWORD

Understanding the protected Keyword: A Guide to Controlled Access in C#

Jordan T Kay
Nerd For Tech
Published in
2 min readAug 22, 2024

--

In object-oriented programming, managing access to class members (such as variables or methods) is crucial for maintaining clean and secure code. The protected keyword in C# is an essential tool for controlling this access, particularly when working with inheritance. But what exactly does protected do, and when should you use it? Let’s break it down.

What is the protected Keyword?

The protected keyword is used to define the accessibility of class members. It strikes a balance between the private and public access modifiers, offering a middle ground that provides flexibility within an inheritance hierarchy while still protecting class data.

Here’s a quick comparison:

  • private: The member is only accessible within the same class. No other class, not even a derived class, can access it.
  • public: The member is accessible from anywhere, whether it’s within the same class, a derived class, or even from outside the codebase.
  • protected: The member is accessible within the same class and by any derived classes that inherit from it, but it’s not accessible from outside the class or by unrelated classes.

When to Use protected

--

--

Jordan T Kay
Nerd For Tech

Join my journey into game development with Unity. Learning all I can to produce quality games. 🚀