Access Modifiers in C# (protected internal)

July8, 2018
by admin

Let’s see in this section another important access modifier ‘protected internal’.

In previous sections, we have seen ‘private’, ‘public’, ‘internal’ and ‘protected’ access modifiers. The one pending access modifier to discuss is ‘protected internal’.

We have seen ‘protected’ and ‘internal’ access modifiers. This ‘protected internal’ access modifier is a combination of both these modifiers. Let us discuss it.

If you have not read the previous articles on access modifiers yet, it is highly recommended to read them first before reading this one. Click below links to read those articles.

Access Modifiers in C# (public, private)

Access Modifiers in C# (internal)

Access Modifiers in C# (protected)

With the ‘internal’ access modifier a class member has following accessibility.

  • It is accessible to everywhere in the project where it is defined.
  • It is not accessible to class members of any other projects.

With the ‘protected’ access modifier a class member has following accessibility.

  • It is accessible to only inherited class members in the project where it is defined.
  • It is accessible to only inherited class members of any other projects.

With the ‘protected internal’ access modifier a class member has following accessibility.

  • It is accessible to everywhere in the project where it is defined. (internal)
  • It is accessible to only inherited class members of any other projects. (protected)

This is the combination of point 1 from the accessibility of ‘internal’ access modifier and point 2 from the accessibility of ‘protected’ access modifier.

Let’s see this with code example.

We are going to work on the same projects as with the previous section.

Let’s look at the project’s existing structure.

Open ‘EmployeeInfo’ project.

Open ‘Employee’ class and change access modifier of ‘PrintEmployeeInfo()’ method from ‘protected’ to ‘protected internal’.

With this change, our ‘Employee’ class code will be as shown below.


namespace EmployeeInfo
{
  public class Employee
  {
    private string _firstName;
    private string _lastName;

    public string FirstName
    {
      get
      {
        return _firstName;
      }
      set
      {
        _firstName = value;
      }
    }

    public string LastName
    {
      get
      {
        return _lastName;
      }
      set
      {
        _lastName = value;
      }
    }

    protected internal void PrintEmployeeInfo()
    {
      Console.WriteLine(string.Format("Name: {0} {1}", _firstName, _lastName));
    }
  }
}

As you can see the only change we have done is having ‘bold’ style font.

We are going to make no code changes in ‘EmployeeProfile’ class which is inherited from ‘Employee’ base class and called ‘PrintEmployeeInfo()’ method of the base class in its own method ‘PrintProfileInfo’.

Look at the code of ‘EmployeeProfile’ class.


namespace EmployeeInfo
{
  public class EmployeeProfile: Employee
  {
    private string _designation;

    public string Designation
    {
      get
      {
        return _designation;
      }
      set
      {
        _designation = value;
      }
    }

    public void PrintProfileInfo()
    {
      PrintEmployeeInfo();
      Console.WriteLine(string.Format("Designation: {0}", _designation));
    }
  }
}

Since ‘PrintEmployeeInfo()’ method is protected it can be accessible to members of ‘Employee’ class’s child class ‘EmployeeProfile’.

Can ‘PrintEmployeeInfo()’ method of ‘Employee’ class can be accessible to any other class within the same project ‘EmployeeInfo’?

The answer is ‘Yes’. This is due to the ‘internal’ part of the access modifier. Let’s verify this with the updated code of ‘EmployeePay’ class as shown below. Modify the existing code with the following code.


namespace EmployeeInfo
{
  public class EmployeePay
  {
    public float Basic { get; set; }

    public EmployeePay()
    {            
    }

    public void PrintPayInfo()
    {
      Employee employee = new Employee();
      employee.FirstName = "James";
      employee.LastName = "Scott";
      employee.PrintEmployeeInfo();

      Console.WriteLine(string.Format("Basic: {0}", Basic));
    }
  }
}

As you can see the changes in bold style font, we have created the object of ‘Employee’ class within the ‘PrintPayInfo()’ method and then we can call its ‘PrintEmployeeInfo()’ method.

This verifies that a class member having ‘protected internal’ access modifier can be accessible to any class member within the project due to ‘internal’ part of the access modifier.

Let’s now save the changes within the ‘EmployeeInfo’ project and then open our main console application project ‘AccessModifierDemo’.

Change the code within the ‘Main’ function of ‘Program’ class as shown below.


namespace AccessModifierDemo
{
  class Program
  {
    static void Main(string[] args)
    {
      EmployeePay pay = new EmployeePay();
      pay.Basic = 2500; 

      pay.PrintPayInfo();

      Console.ReadKey();
    }
  }
}

We have first created an object of ‘EmployeePay’ class and then assign a value to its property ‘Basic’.

EmployeePay pay = new EmployeePay();
pay.Basic = 2500;

The important thing to point out here is that we haven’t set employee’s first name and last name in client code. This is because they are set within the ‘PrintPayInfo()’ method of ‘EmployeePay’ class.

If we want to allow client code to set these properties, then we have to create an object of ’Employee’ class here in the client code. Then set values of FirstName and LastName properties. After that pass it to ’EmployeePay’ object by some way such as passing through constructor parameter (as shown in one of the previous sections of access modifiers) or via method parameter.

After assigning a value of employee’s Basic pay, we have called ‘PrintPayInfo()’ method of ‘EmployeePay’ object.

pay.PrintPayInfo();

This method set the employees first name and last name and prints all the information of employee to console output.

Save the application. Build and run by pressing the key (Ctrl + F5).

We will get the following output.

Output:

Name: James Scott
Basic: 2500

The above demonstration shows the part of the ’protected internal’ access modifier. It shows how a class member can be accessible to anywhere within the project due to its ’internal’ keyword.

Now let’s see the role of ‘protected’ part of ‘protected internal’ access modifier.

Due to this ‘protected’ part, it is accessible to only inherited class members of any other projects. Let’s verify this with a code example.

For this demonstration, we are not going to change the code of ’EmployeeInfo’ project. We are only going to make some code changes within the ’AccessModifierDemo’ project, our main console application project.

Let’s first see whether we can access ‘protected internal’ member within other project or not. For this write the following code to ‘Main’ function of ‘Program’ class.


namespace AccessModifierDemo
{
  class Program
  {
    static void Main(string[] args)
    {
      Employee employee = new Employee();
      employee.FirstName = "James";
      employee.LastName = "Scott";
      employee.PrintEmployeeInfo();  // This won’t work due to protection level.

      Console.ReadKey();
    }
  }
}

In above code, we have created an object of ‘Employee’ class. Then we have assigned values to first name and last name and tried to call ‘PrintEmployeeInfo() method which has a ‘protected internal’ modifier.

This won’t work. If we build the project, we will get the compile time error stating that ‘EmployeeInfo.Employee.PrintEmployeeInfo()’ is inaccessible due to its protection level’.

This verifies that a class member having ‘protected internal’ access modifier cannot be directly accessible to client code within in the other project.

Let’s now verify whether it can be accessible to inherited or child class members of another project. For that let’s make some changes within the ‘EmployeeCategory’ class.

Note that ‘EmployeeCategory’ class is a part of the ‘AccessModifierDemo’ project.

Have a look at the modified code of ‘EmployeeCategory’ class.


using EmployeeInfo;

namespace AccessModifierDemo
{
  public class EmployeeCategory: Employee
  {
    public string Category { get; set; }

    public void PrintEmployeeCategory()
    {
      PrintEmployeeInfo();
      Console.WriteLine(string.Format("Category: {0}", Category));
    }
  }
}

Look at the code having bold style font.

The first thing to notice is that the ’EmployeeCategory’ class is a child class of the ’Employee’ base class.

Next change to notice is a call to ’PrintEmployeeInfo()’ method of the base class ’Employee’ from ’EmployeeInfo’ project having ’protected internal’ access modifier.

If we build the application, we won’t get any error. This means that we have access to the ‘PrintEmployeeInfo()’ method within ‘EmployeeCategory’ class.

Save ‘EmployeeCategory’ class.

Open the ‘Program’ class and change the code of Main() method with the following code.


namespace AccessModifierDemo
{
  class Program
  {
    static void Main(string[] args)
    {
      EmployeeCategory employeeCategory = new EmployeeCategory();

      employeeCategory.FirstName = "Chittaranjan";
      employeeCategory.LastName = "Dhurat";
      employeeCategory.Category = "Senior level";

      employeeCategory.PrintEmployeeCategory();

      Console.ReadKey();
    }
  }
}

Look at the above code. We have first created the object of ‘EmployeeCategory’ class.

EmployeeCategory employeeCategory = new EmployeeCategory();

Next, we have assigned values to employees first name, last name, and category.

employeeCategory.FirstName = "Chittaranjan";
employeeCategory.LastName = "Dhurat";
employeeCategory.Category = "Senior level";

Next, we have called the ‘PrintEmployeeCategory()’ method of ‘EmployeeCategory’ object.

employeeCategory.PrintEmployeeCategory();

This will print employee name and category to console output.

Save the application and build and run by pressing the key (Ctrl + F5).

We will get the following output.

Output:

Name: Chittaranjan Dhurat
Category: Senior level

To close the application either press ‘Enter’ key or click menu item ‘Debug > Stop Debugging’ (Shift + F5) or click stop debugging icon from toolbar, or click ‘Close’ icon from console output window.

With this demonstration, we have verified that a class member having ‘protected internal’ access modifier can only be accessible to inherited (child) class members of any other project.

Summary:

Access modifier Description
protected internal A class member is accessible to everywhere in the project where it is defined. (internal)
A class member is accessible to only inherited class members of any other projects. (protected)

In this section, we have seen one important access modifier in C#, ’protected internal’ with demo code examples.

This completes our discussion on ‘Access modifiers’ in C#. Hope that you understood the concept very well and can use this knowledge in your C# projects.

C# Object Oriented Programming