C# Access Modifiers Tutorial For Beginners With Examples

Types and members in C# have certain accessibility level. This accessibility level is enforced by using the Access modifiers (Also called as Access specifiers).
Access modifiers decide whether a Type or a Member should be visible to other code from inside a assembly or from other assemblies.

Access Modifiers in C#

Following Access modifiers are available in C#:
  • public
  • protected
  • internal
  • protected internal
  • private

Public Access Modifier

Types or type members having a public access modifier can be accessed by any program. There are no restrictions on accessing a public type or member.
    public class Data
    {
        public int axisX;
        public int axisY;
    }

    static void Main(string[] args)
    {
        Data data = new Data();

        data.axisX = 100;
        data.axisY = 300;

        Console.WriteLine("Axis X is {0} and Y is {1}", data.axisX, data.axisY);

        Console.ReadLine();
    }
        
Output:
Axis X is 100 and Y is 200
Types which are directly under a namespace can only be declared as public or internal.

Protected Access Modifier

Protected members are accessible with in the same class and from the derived classes.
In the below example since Employee class is derived from People class hence, Employee class is able to access the protected variable name declared in the People class.
    class People
    {
        protected string name;
    }

    class Employee: People
    {
        public string GetName()
        {
            return name;
        }
    }
        
Members of a struct cannot be declared as protected as structs cannot be derived from other structs.

Internal Access Modifier

If no access modifier is specified then the default access modifier is internal. A class, struct or member with an internal access modifier can only be accessed with in the same assembly.
Lets say there are two files named Math.cs and Business.cs in a C# project. Math.cs contains a class called as Compute as shown below.
    public class Compute
    {
        internal double pi = 3.14;
    }
            
Business.cs has a class called as Systems as shown below.
    class Systems
    {        
        static void Main(string[] args)
        {
            Compute compute = new Compute();
            
            Console.WriteLine("Value of pi is {0}", compute.pi);

            Console.ReadLine();
        }

    }
            
Since the class Compute has an public access modifier hence an object of type Compute can be created inside the class Systems and since the variable pi has an internal access modifier so it is also accessible.
If the access modifier of the variable pi is changed to protected then the class Compute can still be instantiated but the variable pi cannot be accessed.

Protected Internal Access Modifier

A member with protected internal access modifier can be accessed within the same assembly or by the derived types.
    public class Compute
    {
        protected internal double pi = 3.14;
    }
        
Since the above class Compute is declared as public so an instance of it can be created by other assemblies. However, the variable pi can't be accessed directly from other assemblies. To access this variable from an external assembly a class has to derive from Compute.

Private Access Modifier

Members with private access modifier can only be accessed within the body of the class or struct in which they are declared.
    public class Compute
    {
        private double pi = 3.14;

        public double GetPi()
        {
            return pi;
        }
    }            
            
In the below example the instance of class Compute can only access the method GetPi but can't access the member pi as it is private.
    class Systems
    {
        static void Main(string[] args)
        {
            Compute compute = new Compute();
            
            //Below line will work correctly
            Console.WriteLine("Value of pi is {0}", compute.GetPi());

            //Below line will throw error
            Console.WriteLine("Value of pi is {0}", compute.pi());
            Console.ReadLine();
        }
    }
        

Encapsulation in C#:

  • Restricting access to the members of an type is called as Encapsulation. Access modifiers are used to achieve this.
  • This is one of the four fundamentals of Object Oriented Programming (OOP).