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

In c#, Access Modifiers are the keywords which are 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 that whether they can be accessed in other classes or in current assembly or in other assemblies based on our requirements.

 

Following are the different type of access modifiers available in c# programming language.

 

  • Public
  • Private
  • Protected
  • Internal

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

 

Access ModifierDescription
public It is used to specifies that access is not restricted.
private It is used to specifies that access is limited to the containing type.
protected It is used to specifies that access is limited to the containing type or types derived from the containing class.
internal It is used to specifies that access is limited to current assembly.
protected internal It is used to specifies that access is limited to the current assembly or types derived from the containing class.
private protected It is used to specifies 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. In case, 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 types can only have public or internal accessibility. The default accessibility for top level types is internal.

C# Public Access Modifier

In c#, 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 current assembly or another assembly that references it.

 

Following is the example of defining a members with public modifier in 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 Any Key to Exit..");

            Console.ReadLine();

        }

    }

}

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

 

When we execute above c# program, we will get the result like as shown below.

 

C# public access modifier example result 

If you observe above result, we are able to access the variables and methods of User class in another class because of specifying with public specifier 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#, 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 same class or structure.

 

Following is the example of defining a members with private modifier in 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();

            // Complier Error

            // These are inaccessible due to private specifier

            u.Name = "Suresh Dasari";

            u.Location = "Hyderabad";

            u.Age = 32;

            u.GetUserDetails();

            Console.WriteLine("\nPress Any Key to Exit..");

            Console.ReadLine();

        }

    }

}

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

 

When we execute above c# program, we will get a compile time errors like as shown below.

 

c# private access modifier example result

 

If you observe above result, we are getting a compile time errors because the private modifier members of User class 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#, 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 a members with protected modifier in 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 Any Key to Exit..");

            Console.ReadLine();

        }

    }

}

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

 

When we execute above c# program, we will get a compile time errors like as shown below.

 

c# protected access modifier example result

 

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

 

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

 

Following is the example of accessing a base class protected members in derived class through 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();

            // Complier Error

            // protected members can only accessible with derived classes

            //u.Name = "Suresh Dasari";

            p.Name = "Suresh Dasari";

            p.Location = "Hyderabad";

            p.Age = 32;

            p.GetUserDetails();

            Console.WriteLine("\nPress Any Key to Exit..");

            Console.ReadLine();

        }

    }

}

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

 

When we execute above c# program, we will get the result like as shown below.

 

C# protected access modifier example result

 

This is how we can use protected modifier in our c# applications to limit an access of type or member in a 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#, internal modifier is used to specify that access is limited to current assembly so the type or member can be accessed by any code in the same assembly, but not from other assembly.

 

Following is the example of defining a members with internal modifier in 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 Any Key to Exit..");

            Console.ReadLine();

        }

    }

}

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

 

When we execute above c# program, we will get a compile time errors like as shown below.

 

C# internal access modifier example result

 

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

 

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

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, so the type or member can be accessed by any code in the same assembly or by any derived class in another assembly.

 

Following is the example of defining a members with protected internal modifier in 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 Any Key to Exit..");

            Console.ReadLine();

        }

    }

}

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

 

When we execute above c# program, we will get a compile time errors like as shown below.

 

C# protected internal access modifier example result

 

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

 

As discussed, in c# the protected internal type or members are accessible from the current assembly or from the types that are 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 and it is used to specify that access is limited to the containing class or types derived from the containing class within the current assembly, so the type or member can be accessed by code in the same class or in a derived class within the base class assembly.

 

Following is the example of defining a members with private protected modifier in 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();

            // Complier Error

            // protected members can only accessible with derived classes

            //u.Name = "Suresh Dasari";

            p.Name = "Suresh Dasari";

            p.Location = "Hyderabad";

            p.Age = 32;

            p.GetUserDetails();

            Console.WriteLine("\nPress Any Key to Exit..");

            Console.ReadLine();

        }

    }

}

If you observe above example, we are accessing base class (User) private protected members using the reference of derived class (Program) and 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 derived class (Program).

 

When we execute above c# program, we will get the result like as shown below.

 

C# private protected internal access modifier example result

 

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