protected

noun

say it: “pruh-TEK-tid” /prəˈtɛktɪd/

An access modifier that lets a member be used in its own class and in classes derived from it.

What is protected in C#?

protected — An access modifier that lets a member be used inside its own class and in classes that inherit from it, but not from anywhere else.

class Account
{
    protected decimal balance = 20m;
}

class SavingsAccount : Account
{
    public void AddInterest()
    {
        balance += balance * 0.02m;   // allowed: SavingsAccount inherits from Account
    }
}

In a sentence

  • “I made the stock count protected, so Ebook can read it but other classes can't.”

Level: Journeyman · More C# terms at this level

Updated

In other subjects