C# Access Modifiers (Public, Private, Protected, Internal)

What is Access Modifiers ?

Access Modifiers are the keywords which are used to define the accessibility of the class and its members such as method , properties , fields and others members .

➤All the C# types have access modifiers implemented, even if they are not stated (default access modifier is applied then).

Why we are using Access Modifiers ?

➤By specifying an access level , we can control whether they can be accessed in other classes or in current assembly or in other assemblies .

➤To control the visibility of class members (the security level of each individual class and class member).

➤To achieve “Encapsulation” – which is the process of making sure that “sensitive” data is hidden from users. This is done by declaring fields as private.

Access Modifiers Types

C# provides four types of access modifiers:

  1. private
  2. public
  3. protected
  4. internal and two combinations:
  5. protected-internal and
  6. private-protected.

1.Private Access Modifier

Objects that implement private access modifier are accessible only inside a class or a structure. As a result, we can’t access them outside the class they are created:

Example:

class NumberClass
{
    private int number = 10;
}
 
class Program
{
    static void Main(string[] args)
    {
        NumberClass num = new NumberClass();
        Console.WriteLine(num.number); // Error. We can't access the number variable because 
        // it has the private access modifier and its only accessible in the NumberClass class
    }
}

2.Public Access Modifier

Objects that implement public access modifiers are accessible from everywhere in our project. Therefore, there are no accessibility restrictions:

Example:

class NumberClass
{
    public int number = 10;
}
 
class Program
{
    static void Main(string[] args)
    {
        NumberClass num = new NumberClass();
        Console.WriteLine(num.number); // This is OK. The number variable has the public access modifier.
    }
}

3.Protected Access Modifier

The protected keyword implies that the object is accessible inside the class and in all classes that derive from that class. We will talk in more detail about inheritance in module 2 about object-oriented programming. But for now, we are going to take a look at this example to understand the behavior of the protected members:

Example:

class NumberClass
{
    protected int number = 10; //we can access this variable inside this class
}
 
class DerivedClass: NumberClass //this is inheritance. DerivedClass derives from the NumberClass class
{
    void Print()
    {
        Console.WriteLine(number); //we can access it in this class as well because it derives from the NumberClass class
    }
}
 
class Program
{
    void Print()
    {
        NumberClass num = new NumberClass();
        Console.WriteLine(num.number); // Error. The number variable is inaccessible due to its protection level. 
                               // The Program class doesn't derive from the NumberClass
    }
}

4.Internal Access Modifier

The internal keyword specifies that the object is accessible only inside its own assembly but not in other assemblies:

Example:

//First Project (ASSEMBLY)
public class NumberClassInFirstProject
{
    internal int number = 10; //we can access this variable inside this class
}
 
class ProgramInFirstProject
{
    void Print()
    {
        NumberClassInFirstProject num = new NumberClassInFirstProject();
        Console.WriteLine(num.number); // This is OK. Anywhere in this project (assembly) 
                                           // we can access the number variable.
    }
}
 
//Second project (ASSEMBLY)
class Program
{
    void Print()
    {
        NumberClassInFirstProject num = new NumberClassInFirstProject();
        Console.WriteLine(num.number); // Error. The number variable is inaccessible due to its protection level. 
                               //The Program class in second project can't access the internal members from another project
    }
}

5.Protected Internal Access Modifier

The protected internal access modifier is a combination of protected and internal. As a result, we can access the protected internal member only in the same assembly or in a derived class in other assemblies (projects):

Example:

//First Project (ASSEMBLY)
public class NumberClassInFirstProject
{
    protected internal int number = 10; //we can access this variable inside this class
}
 
class ProgramInFirstProject
{
    void Print()
    {
        NumberClassInFirstProject num = new NumberClassInFirstProject();
        Console.WriteLine(num.number); // This is OK. Anywhere in this project (assembly) we can access the number variable.
    }
}
 
//Second project (ASSEMBLY)
class Program: NumberClassInFirstProject //Inheritance
{
    void Print()
    {
        Console.WriteLine(number); //This is OK as well. The class Program derives from the NumberClassInFirstProject clas.
    }
}

6.Private Protected Access Modifier

The private protected access modifier is a combination of the private and protected keywords. We can access members inside the containing class or in a class that derives from a containing class, but only in the same assembly(project). Therefore, if we try to access it from another assembly, we will get an error.

Access ModifierDescription
publicThe code is accessible for all classes
It is used to specifies that access is not restricted.Public access specifier allows a class to expose its member variables and member functions to other functions and objects. Any public member can be accessed from outside the class.
privateIt is used to specifies that access is limited to the containing type. The code is only accessible within the same class.Private access specifier allows a class to hide its member variables and member functions from other functions and objects. Only functions of the same class can access its private members. Even an instance of a class cannot access its private members.
protectedIt is used to specifies that access is limited to the containing type or types derived from the containing class. The code is accessible within the same class, or in a class that is inherited from that class. You will learn more about inheritance in a later chapter.Protected access specifier allows a child class to access the member variables and member functions of its base class. This way it helps in implementing inheritance. We will discuss this in more details in the inheritance chapter
internalIt is used to specifies that access is limited to the current assembly. The code is only accessible within its own assembly, but not from another assembly. You will learn more about this in a later chapter.Internal access specifier allows a class to expose its member variables and member functions to other functions and objects in the current assembly. In other words, any member with internal access specifier can be accessed from any class or method defined within the application in which the member is defined.
protected internalIt is used to specifies that access is limited to the current assembly or types derived from the containing class.The protected internal access specifier allows a class to hide its member variables and member functions from other class objects and functions, except a child class within the same application. This is also used while implementing inheritance.
private protectedIt is used to specifies that access is limited to the containing class or types derived from the containing class within the current assembly. private protected really means protected AND also internal. That is – member is accessible only to child classes which are in the same assembly, but not to child classes which are outside assembly

PUBLIC
PROTECTEDINTERNALPROTECTED INTERNALPRIVATEPRIVATE PROTECTED
Entire programYesNoNoNoNoNo
Containing classYesYesYesYesYesYes
Current assemblyYesNoYesYesNoNo
Derived typesYesYesNoYesNoNo
Derived types within current assemblyYesYesNoYesNoYes

public Accessibility Level

Why Access Modifiers?

Note: By default, all members of a class are private if you don’t specify an access modifier: