Access Modifiers in C# (internal)

December28, 2017
by admin

In the last section we have learned about public and private access modifiers. Let’s now discuss about internal access modifier.

For this section, we are continue using our existing project that we have created in the first section. If you haven’t read that then it is highly recommended to read it first before continuing with this. To read it, click the following link.

Access Modifiers in C# (public, private)

What is an internal access modifier?

When a class member has internal access modifier it is accessible to any other class member within the same project. It cannot be accessible to any class members of other projects.

In other words, the member with internal access modifier is same as public but only within the project.

For example, suppose we have a member ’x’ having internal access modifier in class ’C1’ in project ’P1’, then it is accessible to all other class members of project P1. But it can’t be accessible to any other projects such as P2 and P3.

The keyword for internal access modifier is ‘internal’. Let’s see its use with code example.

We already have an Employee class within the ’EmployeeInfo’ project. The first change for this demonstration is to add a new method ‘PrintEmployeeInfo()’ in Employee class as shown below.

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

This function or method just prints employee name to console window.

Note that we have assigned ’internal’ access modifier to this method. That means it should be accessible to any class within the ’EmployeeInfo’ project including inherited classes of Employee base class.

The complete code of Employee class is 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;
      }
    }

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

  }
}

To demonstrate that a class member having internal access modifier is accessible to any other class within the same project, let’s add a new class ’EmployeePay’ to ’EmployeeInfo’ project.

Right click on ‘EmployeeInfo’ project and click ‘Add > Class’ menu item.

Change the name of the class as ’EmployeePay’ and click ’Add’ button. This will create a class file ’EmployeePay.cs’.

The purpose of ‘EmployeePay’ class is to store and retrieve the employee payment information.

Add the property ‘Basic’ to ‘EmployeePay’ class.

private float _basic;

public float Basic
{
  get
  {
    return _basic;
  }
  set
  {
    _basic= value;
  }
}

To keep demonstration simple, we have added a single property ’Basic’ to ’EmployeePay’ class.

Add a parameterized constructor to ’EmployeePay’ class and pass employee object to it. To store passed employee object we have used a private field ’_employee’.

private Employee _employee; 

public EmployeePay(Employee employee)
{
  _employee = employee;
}

Add the following method ‘PrintPayInfo() to ‘EmployeePay’ class.

public void PrintPayInfo()
{
   _employee.PrintEmployeeInfo();
  Console.WriteLine(string.Format("Basic: {0}", Basic));
}

This ‘PrintPayInfo()’ method first call ‘PrintEmployeeInfo()’ method of employee object to print employee name. Then it prints basic pay value to console window.

The complete code of ‘EmployeePay’ class is shown below.


namespace EmployeeInfo
{
  public class EmployeePay
  {
    private float _basic;
    private Employee _employee;

    public EmployeePay(Employee employee)
    {
      _employee = employee;
    }

    public float Basic
    {
      get
      {
        return _basic;
      }
      set
      {
        _basic = value;
      }
    }

    public void PrintPayInfo()
    {
      _employee.PrintEmployeeInfo();
      Console.WriteLine(string.Format("Basic: {0}", Basic));
    }
  }
}

Note here that we have called ’PrintEmployeeInfo()’ method of Employee object (which has an internal access modifier) from ‘EmployeePay’ class. This is possible because both ’Employee’ and ’EmployeePay’ classes are within the same project ’EmployeeInfo’.

You may ask that why we have used a constructor in ’EmployeePay’ class and pass an ’Employee’ object as a parameter. This is because any project referencing ’EmployeeInfo’ project and used both ’Employee’ and ’EmployeePay’ objects, can’t call ’PrintEmployeeInfo()’ method of ’Employee’ object.

Why? Because ’PrintEmployeeInfo()’ method has an internal access modifier and can’t accessible outside the ’EmployeeInfo()’ project.

So to print the name of an employee, we have delegated the responsibility of calling the ’PrintEmployeeInfo()’ method of ’Employee’ object to ’EmployeePay’ object. Whenever client code has to print employee name along with the payment information, it has to pass ’Employee’ object to the constructor of ’EmployeePay’ class and need to call PrintPayInfo() method of it to print employee name and basic pay amount.

A client code is any code that uses these classes objects. In this case, code within the Main() method of Program class in ’AccessModifierDemo’ project is a client code.

Let’s add the following code to the Main() method of Program class of ‘AccessModifierDemo’ project.

Create an object of Employee class.

Employee employee = new Employee();

Then assign the first name and last name of an employee.

employee.FirstName = "Chittaranjan";
employee.LastName = "Dhurat";

Next, create a new object of ’EmployeePay’ class and pass ’Employee’ object to its constructor.

EmployeePay pay = new EmployeePay(employee);

Then, assign the value of basic pay of an employee.

pay.Basic = 2500;            
pay.PrintPayInfo();

Now call ‘PrintPayInfo()’ method of ‘EmployeePay’ object which will print basic pay value to console window. It will also call the PrintEmployeeInfo() method of ‘Employee’ object to print the first name and last name of the employee.

The complete code of Program class is as follows.


using System;
using EmployeeInfo;

namespace AccessModifierDemo
{
  class Program
  {
    static void Main(string[] args)
    {
      Employee employee = new Employee();
      employee.FirstName = "Chittaranjan";
      employee.LastName = "Dhurat";

      EmployeePay pay = new EmployeePay(employee);
      pay.Basic = 2500;            
      pay.PrintPayInfo();
  
      Console.ReadKey();
    }
  }
}

Save the project and run the application by pressing key ‘F5’.

As expected we will get the following output displaying employee’s name and and basic pay.

Output:

Name: Chittaranjan Dhurat
Basic: 2500

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.

Important point to note here is that we are calling PrintPayInfo() method of Employee object.
This first call the PrintEmployeeInfo() method of employee object (which we have passed via constructor) which prints the name of the employee. The object of ’EmployeePay’ class can access ’PrintEmployeeInfo()’ method of the object of ’Employee’ class because ’PrintEmployeeInfo()’ method has an internal access modifier and both ’Employee’ and ’EmployeePay’ classes belong to the same project. That’s the reason we need to pass ’Employee’ object to ’EmployeePay’ object from client code.

If we try to access the ’PrintEmployeeInfo()’ method of ’Employee’ object from client code then it will fail since it is not accessible outside ’EmployeeInfo’ project.

So the following code will fail.

Employee employee = new Employee();

employee.FirstName = “Chittaranjan”;
employee.LastName = “Dhurat”;

employee.PrinfEmployeeInfo(); // will NOT work

Summary:

Access modifier Description
internal A class member can be accessed by any other class member within the same project.
But it is not accessible to any other project members.

In this section, we have covered the concept of ‘internal’ access modifier.

C# Object Oriented Programming