The protected keyword is an access modifier applied to the declaration of a class and any of its members that restricts the scope of accessibility to either within the class (or struct) where it is declared or from classes derived (inherited) from it.

 protected exampleObj; 

1. General

C# shares the protected keyword with several other languages, such as Java.

2. What is the protected keyword?

The protected keyword is a member of the access modifier family.

Modifier keywords specify who can access and modify types and type members. In other words, you use them to control which parts of your program can be modified by other parts.

Access modifiers can be applied to the declaration of a class and to any of its members such as properties, fields and methods.

You can read more about C# Access Modifier Keywords here (coming soon).

Apart from protected, the access modifier family has five other members

  1. public,
  2. private,
  3. protected Internal,
  4. internal
  5. Private Protected (C# version 7.2 and later)

Using the protected modifier means that you can no longer access the element directly with any random instance.

The field is accessible either within its native class or via its derived class' instance.

3. How to use the "Protected" Keyword?

To implement the protected keyword successfully, you can refer to the examples described below.

A protected variable is defined by the protected keyword at the beginning of its definition.

Example 1


class parent
{
    protected int age = 25;
}

class child : parent
{
    static void Main()
    {
        var parentObj = new parent();
        var childObj = new child();

        childObj.age = 20;
    }
}
 

Explanation:

In the above program, you can see that I have created a class parent in which a variable is declared, i.e age as protected.

Later, I have created another class child that inherits the properties of the parent class.

Now, to access the protected variable age of the parent class in the child’s main function, I have to create an instance of the child class.

You cannot access the age variable through the parent class' instance as that will void the norms of the protected keyword.

As mentioned above, a protected element can be accessed either from its base or via its derived class' instance. Since I wanted to access the age variable outside the scope of its base class, I had to use the instance of the child class.

Example 2:



// class 1
public class elements
{
    protected string protectedString;
    public string publicString;
}

// class 2
public class child : elements
{
    public string description
    {
        get { return this.protectedString; }
    }
}

...

var elementsObj = new elements();
var result1 = eleObj.publicString;

var childObj = new child();
var result2 = childObj.description;
 

Explanation:

In this example, I have created a class elements which has two variables of string type. One of them is public whereas the other one is protected. Next, I made another class child which has a method description where the protected variable is returned.

I have created instances of both parent and child class, but to access the protected variable of the parent class outside its scope, I have used the instance of the child class.

Attempting to access the variable from the parent class instance would have returned a compilation error.

4. When to use the protected keyword?

The main purpose of using protected keywords is to ensure the security of some elements.

Since any protected element can't be accessed directly without an appropriate instance, an extra security layer is added to the data of that C# program.

Generally, it is used so that the child class can inherit (securely) the properties and elements of the parent class. They may contain valuable information that should be accessed with appropriate authorization only

They are typically used to store information for the class. These elements are actually never created with a motive to be accessed in the main function.

The protected keyword allows you to make your code more reusable and conform more robustly to the object-oriented model.

5. Final Notes

The concept of protected keyword can help you develop more secure programs as it adds an extra security layer to the elements it is used with.