C# Access Modifiers

The C# Access specifiers or access modifiers can be used to define the scope of a type and its members. In general, a member of a class that defined with any scope is always accessible within the class.

The restrictions will be applicable when we go outside the class. C# supports five access modifiers. They are

  • Private
  • Public
  • Internal
  • Protected
  • Protected Internal.

So the class members defined with any of the above-defined scopes are always accessible within the C# class.

C# Access Modifiers CASE 1: Accessible members of the class within the class

using System;

namespace CSharp_Tutorial
{
    class Program
    {
        public void demo()
        {
            Console.WriteLine("public method");
        }
        private void demo1()
        {
            Console.WriteLine("private method");
        }
        internal void demo2()
        {
            Console.WriteLine("internal method");
        }
        protected void demo3()
        {
            Console.WriteLine("protected method");
        }
        protected internal void demo4()
        {
            Console.WriteLine("Protected internal method");
        }
        static void Main(string[] args)
        {
            Program c = new Program();
            c.demo(); c.demo1(); c.demo2(); c.demo3(); c.demo4();
            Console.ReadLine();
        }
    }
}

OUTPUT

C# Access Modifiers 1

All the C# members with any scope can be accessed within the class.

C# Access Modifiers Points to remember

  • The C# members declared as private are accessible only within the class.
  • The default scope for the members of a class is private.
  • We cannot declare types as private, and we cannot apply private on a class.
  • Only public and internal can be used in a class, whereas protected, protected internal and private cannot be applied to a class.
  • If we don’t apply the public to a class, it will become an internal class.

C# Access Specifiers CASE 2: Accessing members of a class using the child class

Let us now create another class in the same namespace CSharp_Tutorial with the classname Program2.

using System;
using System.Collections.Generic;
using System.Text;

namespace CSharp_Tutorial
{
    class Program2:Program
    {
        static void Main()
        {
            Program2 p = new Program2();
            p.demo(); p.demo2(); p.demo3(); p.demo4();
            Console.ReadLine();

        }

    }
}

OUTPUT

C# Access Modifiers 2

Since private methods can be accessed only within the class, we tried to access the methods with all the scopes except private in the child class.

C# Access Specifiers CASE 3: Accessing members of the class using a non-child class of the same project

Let us now create another class Program3 in the same namespace and try to access the methods of class Program by creating an instance for a class Program in class Program3.

using System;
using System.Collections.Generic;
using System.Text;

namespace CSharp_Tutorial
{
    class Program3
    {
        static void Main()
        {
            Program p = new Program();
            p.demo(); p.demo2(); p.demo4();
            Console.ReadLine();
        }
    }
}

OUTPUT

C# Access specifiers 3

Here, we had accessed the members of Program in Program3 by creating an instance for the Program in Program3. 

But here we had observed that only members with the scope public, internal, protected internal could be accessed in Program3 since it is a non-child class for Program.

C# Access Modifiers CASE 4: Accessing members of a class using child class of different project

Here, in this case, C# members with the scope of Public, Protected, and Protected internal are accessible outside the project. Still, the members with the scope internal cannot be accessed.

Accessing the members of a C# class outside the project can be done by adding the project’s reference containing the members to the project in which the members are to be accessed.

And once the reference added, we can access the members either through inheritance or by creating the instance.

Remember, every class can be accessed within the project but not outside the project until it changed to the public. That is the reason for saying, “the default scope for a class is internal.”

C# Access Modifiers CASE 5

Accessing C# members of a class using a non-child class of different project.

Only members with the scope public can be accessed using the non-child class of different projects.

i.e., from the example discussed in case 1, only demo() can be accessed outside the project using a non-child class.

C# Access specifiers 4