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

In c#, Access Modifiers are the keywords used to define an accessibility level for all types and type members.

 

By specifying an access level for all types and type members, we can control whether they can be accessed in other classes or the current assembly or other assemblies based on our requirements.

 

The following are the different types of access modifiers available in the c# programming language.

 

  • Public
  • Private
  • Protected
  • Internal

Using these four access modifiers, we can specify the following six levels of accessibility for all types and type members based on our requirements.

 

Access ModifierDescription
public It is used to specify that access is not restricted.
private It is used to specify that access is limited to the containing type.
protected It is used to specify that access is limited to the containing type or types derived from the containing class.
internal It is used to specify that access is limited to the current assembly.
protected internal It specifies that access is limited to the current assembly or types derived from the containing class.
private protected It is used to specify that access is limited to the containing class or types derived from the containing class within the current assembly.

Generally, in c# only one access modifier is allowed to use with any member or type, except when we use protected internal or private protected combinations.

 

In c#, we are not allowed to use any access modifiers on namespaces because the namespaces have no access restrictions.

 

Only certain access modifiers are allowed to specify based on the context in which a member declaration occurs. If we didn’t mention any access modifiers during member declaration, then the default access modifiers will be used depending on the member declaration context.

 

For example, the top-level types which are not nested in any other type can only have public or internal accessibility. The default accessibility for top-level types is internal.

C# Public Access Modifier

In c#, the public modifier is used to specify that access is not restricted, so the defined type or member can be accessed by any other code in the current assembly or another assembly that references it.

 

Following is the example of defining members with a public modifier in the c# programming language.

 

using System;

namespace Tutlane
{
    class User
    {
       public string Name;
       public string Location;
       public int Age;
       public void GetUserDetails()
       {
           Console.WriteLine("Name: {0}", Name);
           Console.WriteLine("Location: {0}", Location);
           Console.WriteLine("Age: {0}", Age);
       }
    }
    class Program
    {
        static void Main(string[] args)
        {
            User u = new User();
            u.Name = "Suresh Dasari";
            u.Location = "Hyderabad";
            u.Age = 32;
            u.GetUserDetails();
            Console.WriteLine("\nPress Enter Key to Exit..");
            Console.ReadLine();
        }
    }
}

If you observe the above example, we defined a User class with required variables and method using public access modifier and tried to access those variables and method in another class with an object reference of the User class.

 

When you execute the above c# program, you will get the result below.

 

C# public access modifier example result 

If you observe the above result, we are able to access the variables and methods of the User class in another class because of specifying with public specifiers based on our requirements.

 

As discussed, the public access specifier will make all the defined members or types available to all the types in our application.

C# Private Access Modifier

In c#, the private modifier is used to specify that access is limited to the containing type, so the defined type or member can only be accessed by the code in the same class or structure.

 

Following is the example of defining members with a private modifier in the c# programming language.

 

using System;

namespace Tutlane
{
    class User
    {
       private string Name;
       private string Location;
       private int Age;
       private void GetUserDetails()
       {
           Console.WriteLine("Name: {0}", Name);
           Console.WriteLine("Location: {0}", Location);
           Console.WriteLine("Age: {0}", Age);
       }
    }
    class Program
    {
       static void Main(string[] args)
       {
           User u = new User();
           // Compiler Error
           // These are inaccessible due to private specifier
           u.Name = "Suresh Dasari";
           u.Location = "Hyderabad";
           u.Age = 32;
           u.GetUserDetails();
           Console.WriteLine("\nPress Enter Key to Exit..");
           Console.ReadLine();
       }
    }
}

If you observe the above example, we defined a User class with required variables and method using a private access modifier and tried to access those variables and method in another class with an object reference of the User class.

 

When you execute the above c# program, you will get compile-time errors like as shown below.

 

c# private access modifier example result

 

If you observe the above result, we are getting compile-time errors because the private modifier members of the User class are referred in another class.

 

As discussed, the private modifier type or member can be accessed only by code in the same class or structure.

C# Protected Access Modifier

In c#, the protected modifier is used to specify that access is limited to the containing type or types derived from the containing class, so the type or member can only be accessed by code in the same class or in a derived class.

 

Following is the example of defining members with a protected modifier in the c# programming language.

 

using System;

namespace Tutlane
{
     class User
     {
         protected string Name;
         protected string Location;
         protected int Age;
         protected void GetUserDetails()
         {
             Console.WriteLine("Name: {0}", Name);
             Console.WriteLine("Location: {0}", Location);
             Console.WriteLine("Age: {0}", Age);
         }
     }
     class Program
     {
         static void Main(string[] args)
         {
             User u = new User();
             // Complier Error
             // These are inaccessible due to protected specifier
             u.Name = "Suresh Dasari";
             u.Location = "Hyderabad";
             u.Age = 32;
             u.GetUserDetails();
             Console.WriteLine("\nPress Enter Key to Exit..");
             Console.ReadLine();
         }
     }
}

If you observe the above example, we defined a User class with required variables and method using a protected access modifier and tried to access those variables and method in another class with an object reference of the User class.

 

When you execute the above c# program, you will get compile-time errors like as shown below.

 

c# protected access modifier example result

 

If you observe the above result, we are getting compile-time errors because the protected modifier members of the User class are referred in another class.

 

As discussed, the protected members of a base class can be accessible in the derived class only when access occurs through the derived class type.

 

Following is the example of accessing a base class protected members in a derived class through a derived class type.

 

using System;

namespace Tutlane
{
    class User
    {
        protected string Name;
        protected string Location;
        protected int Age;
        protected void GetUserDetails()
        {
           Console.WriteLine("Name: {0}", Name);
           Console.WriteLine("Location: {0}", Location);
           Console.WriteLine("Age: {0}", Age);
        }
    }
    class Program: User
    {
        static void Main(string[] args)
        {
           User u = new User();
           Program p = new Program();
           // Compiler Error
           // protected members can only be accessible with derived classes
           //u.Name = "Suresh Dasari";
           p.Name = "Suresh Dasari";
           p.Location = "Hyderabad";
           p.Age = 32;
           p.GetUserDetails();
           Console.WriteLine("\nPress Enter Key to Exit..");
           Console.ReadLine();
        }
    }
}

If you observe the above example, we are accessing base class (User) protected members using the reference of the derived class (Program). If we uncomment the commented code, we will get a compile-time error because we are trying to access protected members with a base class (User) reference instead of the derived class (Program).

 

When you execute the above c# program, you will get the result below.

 

C# protected access modifier example result

 

This is how you can use protected modifiers in our c# applications to limit access to a type or member in the same class or derived class based on our requirements. 

 

In c#, the struct members cannot be protected because the struct cannot be inherited.

C# Internal Access Modifier

In c#, the internal modifier is used to specify that access is limited to the current assembly. The type or member can be accessed by any code in the same assembly but not from another assembly.

 

Following is the example of defining the members with an internal modifier in the c# programming language.

 

using System;

namespace Tutlane
{
     class User
     {
        internal string Name;
        internal string Location;
        internal int Age;
        internal void GetUserDetails()
        {
            Console.WriteLine("Name: {0}", Name);
            Console.WriteLine("Location: {0}", Location);
            Console.WriteLine("Age: {0}", Age);
        }
     }
     class Program
     {
        static void Main(string[] args)
        {
            User u = new User();
            u.Name = "Suresh Dasari";
            u.Name = "Suresh Dasari";
            u.Location = "Hyderabad";
            u.Age = 32;
            u.GetUserDetails();
            Console.WriteLine("\nPress Enter Key to Exit..");
            Console.ReadLine();
        }
     }
}

If you observe the above example, we defined a User class with required variables and method using internal access modifier and tried to access those variables and method in another class with an object reference of the User class.

 

When you execute the above c# program, you will get the result below.

 

C# internal access modifier example result

 

If you observe the above result, we are able to access the variables and methods of the User class in another class because of specifying an internal specifier based on our requirements.

 

As discussed in c# the internal type or members are accessible within the same assembly files.

C# Protected Internal Access Modifier

In c#, the protected internal modifier is used to specify that access is limited to the current assembly or types derived from the containing class. The type or member can be accessed by any code in the same assembly or any derived class in another assembly.

 

Following is the example of defining members with a protected internal modifier in the c# programming language.

 

using System;

namespace Tutlane
{
    class User
    {
        protected internal string Name;
        protected internal string Location;
        protected internal int Age;
        protected internal void GetUserDetails()
        {
            Console.WriteLine("Name: {0}", Name);
            Console.WriteLine("Location: {0}", Location);
            Console.WriteLine("Age: {0}", Age);
        }
    }
    class Program
    {
       static void Main(string[] args)
       {
          User u = new User();
          u.Name = "Suresh Dasari";
          u.Name = "Suresh Dasari";
          u.Location = "Hyderabad";
          u.Age = 32;
          u.GetUserDetails();
          Console.WriteLine("\nPress Enter Key to Exit..");
          Console.ReadLine();
       }
    }
}

If you observe the above example, we defined a User class with required variables and method using a protected internal access modifier and tried to access those variables and method in another class with an object reference of the User class.

 

When you execute the above c# program, we will get the result below.

 

C# protected internal access modifier example result

 

If you observe the above result, we are able to access the variables and methods of the User class in another class because of specifying an internal specifier based on our requirements.

 

As discussed in c# the protected internal type or members are accessible from the current assembly or the types derived from the containing class in another assembly.

C# Private Protected Access Modifier

In c#, the private protected modifier is available from version 7.2. It is used to specify that access is limited to the containing class or types derived from the containing class within the current assembly. The type or member can be accessed by code in the same class or a derived class within the base class assembly.

 

Following is the example of defining members with a private protected modifier in the c# programming language.

 

using System;

namespace Tutlane
{
    class User
    {
       private protected string Name;
       private protected string Location;
       private protected int Age;
       private protected void GetUserDetails()
       {
          Console.WriteLine("Name: {0}", Name);
          Console.WriteLine("Location: {0}", Location);
          Console.WriteLine("Age: {0}", Age);
       }
    }
    class Program: User
    {
       static void Main(string[] args)
       {
           User u = new User();
           Program p = new Program();
           // Compiler Error
           // protected members can only be accessible with derived classes
           //u.Name = "Suresh Dasari";
           p.Name = "Suresh Dasari";
           p.Location = "Hyderabad";
           p.Age = 32;
           p.GetUserDetails();
           Console.WriteLine("\nPress Enter Key to Exit..");
           Console.ReadLine();
       }
    }
}

If you observe the above example, we are accessing base class (User) private protected members using the reference of the derived class (Program). If we uncomment the commented code, we will get a compile-time error because we are trying to access private protected members with base class (User) reference instead of the derived class (Program).

 

When you execute the above c# program, you will get the result below.

 

C# private protected internal access modifier example result

 

This is how we can use a private protected modifier in c# applications to limit the containing class or types derived from the containing class within the current assembly.