Skip to main content

How to Make a Member Visible to Derived Classes with protected in C#

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

protected bool IsOn { get; set; } in a base class shares IsOn with the family only: the class itself and every class derived from it can use it, and any other code that tries gets a build error.

Microsoft's reference: The protected keyword is a member access modifier. And: You can access a protected member within its class and by derived class instances. It sits between public and private, which the reference describes as public: Access isn't restricted. and private: Access is limited to the containing type.

Syntax​

Syntax
class <BaseClass>
{
protected <type> <Member> { get; set; }
}

class <DerivedClass> : <BaseClass>
{
// code here can use <Member>
}
PartWhat it is
protectedThe access modifier: <BaseClass> and the classes derived from it can use the member
<type> <Member>The member: a property, as here, or a field or a method
: <BaseClass>Makes <DerivedClass> a derived class, so its code can use <Member>

One rule surprises people: You can access a protected member of a base class in a derived class only if the access occurs through the derived class type. Common Mistakes shows what that means.

The Example​

A console app called ProtectedMember has an Appliance class with a protected IsOn property, and a Kettle class derived from it that switches itself on.

Prerequisites​

  • The .NET 10 SDK. dotnet --version prints a version that starts with 10.0.

Step 1: Create a Console App​

Terminal
dotnet new console -n ProtectedMember
cd ProtectedMember
Output
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring /Users/you/source/ProtectedMember/ProtectedMember.csproj:
Determining projects to restore...
Restored /Users/you/source/ProtectedMember/ProtectedMember.csproj (in 44 ms).
Restore succeeded.

dotnet new console -n ProtectedMember creates a folder named ProtectedMember with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or ProtectedMember.csproj in Visual Studio.

Step 2: Write the Code​

Replace everything in Program.cs. First the code that uses the kettle:

Program.cs (the code that runs)
Kettle kettle = new();
Console.WriteLine($"Kettle: {kettle.Status()}");

kettle.Boil();
Console.WriteLine($"Kettle: {kettle.Status()}");

It prints the kettle's status, boils it, and prints the status again. It never touches IsOn: this code isn't inside Appliance or a class derived from it.

Then the base class:

Program.cs (the base class)
class Appliance
{
// protected: Appliance and the classes derived from it can use IsOn. Other code can't.
protected bool IsOn { get; set; }

public string Status() => IsOn ? "on" : "off";
}

IsOn is protected. Status is in the same class, so it can read IsOn: IsOn ? "on" : "off" returns "on" when IsOn is true and "off" when it's false. Status itself is public, so any code can call it.

Then the derived class, at the end of the file:

Program.cs (the derived class)
class Kettle : Appliance
{
public void Boil()
{
// Allowed: Kettle derives from Appliance.
IsOn = true;
Console.WriteLine("Boiling water...");
}
}

Kettle : Appliance makes Kettle a derived class, so Boil can set IsOn directly.

Step 3: Run It​

Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:

Terminal
dotnet run
Output
Kettle: off
Boiling water...
Kettle: on

macOS-style terminal window titled Terminal — ProtectedMember showing dotnet run printing Kettle: off, Boiling water... and Kettle: on

The status went from off to on: Boil, in the derived class, changed the protected property, and Status, in the base class, read it. The code at the top reached IsOn only through those two public methods.

The Complete File​

macOS-style code window titled Program.cs — ProtectedMember showing a kettle that prints its status, boils and prints its status again, an Appliance class with a protected IsOn property and a public Status method, and a Kettle class derived from Appliance whose Boil method sets IsOn to true

Program.cs
Kettle kettle = new();
Console.WriteLine($"Kettle: {kettle.Status()}");

kettle.Boil();
Console.WriteLine($"Kettle: {kettle.Status()}");

class Appliance
{
// protected: Appliance and the classes derived from it can use IsOn. Other code can't.
protected bool IsOn { get; set; }

public string Status() => IsOn ? "on" : "off";
}

class Kettle : Appliance
{
public void Boil()
{
// Allowed: Kettle derives from Appliance.
IsOn = true;
Console.WriteLine("Boiling water...");
}
}

Common Mistakes​

MistakeWhat happensDo this instead
Using the member from outside the class familykettle.IsOn = true; in the top-level code doesn't build: error CS0122: 'Appliance.IsOn' is inaccessible due to its protection levelGive the class a public method that does the job, like Boil
Using it through a variable of the base type, inside the derived classIn Kettle, other.IsOn = true; with other declared as Appliance doesn't build: error CS1540: Cannot access protected member 'Appliance.IsOn' via a qualifier of type 'Appliance'; the qualifier must be of type 'Kettle' (or derived from it)Use IsOn on the object itself, as Boil does, or through a Kettle variable, as the error says
A protected member in a structerror CS0666: 'Point.X': new protected member declared in structUse private or public. The reference: You can't declare struct members as protected because structs can't be inherited.

FAQ​

What about protected internal and private protected?​

They're two more access levels. The reference notes that The protected keyword is also part of the protected internal and private protected access modifiers. It compares them: Unlike private protected, the protected access modifier allows access from derived classes in any assembly. Unlike protected internal, it doesn't allow access from non-derived classes within the same assembly. Microsoft's access modifiers guide defines the term: An assembly is a .dll or .exe created by compiling one or more .cs files in a single compilation.

Conclusion​

Make a member protected when the classes derived from yours need it but other code shouldn't touch it. Inside the class family, use it directly; for everyone else, offer public methods, like Boil and Status here.

Additional Resources​