C#

Access Modifiers in C#

Programmingempire

In this post on Access Modifiers in C#, I will explain various types of access modifiers in C# that we can use with a class and members of the class. Basically, an access modifier is a keyword that determines where a class member is visible in the application.

In brief, the access modifiers determine the accessibility level of a type member. However, the types themselves can also an access modifier. Accordingly, the type is accessible in the current assembly or any other assembly.

Access Modifiers in C#

To begin with, the access modifiers are applicable to the types as well as the type members. However, only internal and public access modifiers are applicable to a class.

The Following are the access modifiers that are applicable to a type member.

  1. private
  2. protected
  3. internal
  4. protected internal
  5. public
  6. private protected

These accessed modifiers are explained below in brief.

private

If a type member is declared as private, it is accessible only in the containing class. Therefore, only a method of the containing class can access a private type member.

protected

On the other hand, a protected type member is accessible in the containing class as well as in its subclass. Moreover, the subclass can be present in the current assembly as well as in any other assembly.

internal

Basically, the internal keyword is used to indicate that a type member is only visible in the current assembly. Therefore, the internal class members are visible in the containing class, any subclass of the containing class in the current assembly as well as any non-subclass of the current assembly.

protected internal

As can be seen, the protected keyword indicates the accessibility of a class member in a subclass. However, the protected internal access modifier specifies that the type member is accessible only in the containing class as well as in any subclass of the containing class in the current assembly.

public

As its name suggests, the public members of a class are visible everywhere. In other words, if we use the public access modifier with a type or type member, then it will be available in both the current assembly or any other assembly which is referencing the one containing the public member.

private protected

Lastly, we also have private protected access modifier, that is available in C# starting from C# 7.0. Basically, it allows a type member to be visible in the containing class as well as any subclass of the containing class residing in the current assembly.

Examples of Access Modifiers in C#

Given that, we have above mentioned six access modifiers for class members, let us use them in a program. Therefore, we define a base class called MyBaseClass and declare an instance variable with each of these six access modifiers. Further, we create a derived class of the above class and another non-derived class and accessed those fields in these classes.

using System;
namespace AccessModifiersDemo
{
    class MyBaseClass
    {
        private int a = 1;
        protected int b = 2;
        public int c = 3;
        internal int d = 4;
        protected internal int e = 5;
        private protected int f = 6;

        public MyBaseClass()
        {
            Console.WriteLine("Inside Base Class Constructor!");
            Console.WriteLine("a = " + a);
            Console.WriteLine("b = " + b);
            Console.WriteLine("c = " + c);
            Console.WriteLine("d = " + d);
            Console.WriteLine("e = " + e);
            Console.WriteLine("f = " + f);
        }
    }
    class Derived1: MyBaseClass
    {
        public Derived1()
        {
            Console.WriteLine("Inside Derived1 Class Constructor!");
          //  Console.WriteLine("a = " + a);
            Console.WriteLine("b = " + b);
            Console.WriteLine("c = " + c);
            Console.WriteLine("d = " + d);
            Console.WriteLine("e = " + e);
            Console.WriteLine("f = " + f);
        }
    }
    class NonDerived1
    {
        public NonDerived1()
        {
            Console.WriteLine("Inside NonDerived1 Class Constructor!");
            MyBaseClass o = new MyBaseClass();
           // Console.WriteLine("a = " + o.a);
           // Console.WriteLine("b = " + o.b);
            Console.WriteLine("c = " + o.c);
            Console.WriteLine("d = " + o.d);
            Console.WriteLine("e = " + o.e);
           // Console.WriteLine("f = " + o.f);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            MyBaseClass ob = new MyBaseClass();
            Derived1 ob1 = new Derived1();
            NonDerived1 ob2 = new NonDerived1();
        }
    }
}

Output

The following output shows that the derived class in the same assembly can access all members except the private one, while the non-derived class can access the public, internal, and the protected internal members only.

Demonstration of Access Modifiers within Current Assembly
Demonstration of Access Modifiers within Current Assembly

Accessing a Class and Class Members in Another Assembly

As an illustration for this scenario, we create a Class Library Project in Visual Studio and create the same class as above with six fields having each type of access modifier. After that, we compile the class library project.

using System;

namespace ClassLibrary1
{
    public class MyBaseClass
    {
        private int a = 1;
        protected int b = 2;
        public int c = 3;
        internal int d = 4;
        protected internal int e = 5;
        private protected int f = 6;

        public MyBaseClass()
        {
            Console.WriteLine("Inside Base Class Constructor!");
            Console.WriteLine("a = " + a);
            Console.WriteLine("b = " + b);
            Console.WriteLine("c = " + c);
            Console.WriteLine("d = " + d);
            Console.WriteLine("e = " + e);
            Console.WriteLine("f = " + f);
        } L
    }
}

Once our class library project is compiled successfully, we create a Console Application in C#. Further, add the reference of the above class library project in this console application so that MyBaseClass is available here. It is important to remember that now MyBaseClass is declared as public in the class library, otherwise, it is not available in any other assembly.

In like manner, we create a derived class as well as a non-derived class in the console application and accessed the fields that the base class contains.

using System;
using ClassLibrary1;
namespace AccessModifierDemo1
{
    class Derived2: MyBaseClass
    {
        public Derived2()
        {
            Console.WriteLine("Inside Derived2 Class Constructor!");
            //Console.WriteLine("a = " + a);
            Console.WriteLine("b = " + b);
            Console.WriteLine("c = " + c);
            //Console.WriteLine("d = " + d);
            Console.WriteLine("e = " + e);
            //Console.WriteLine("f = " + f);
        }
    }
    class NonDerived1
    {
        public NonDerived1()
        {
            Console.WriteLine("Inside NonDerived1 Class Constructor!");
            MyBaseClass ob = new MyBaseClass();
            //Console.WriteLine("a = " + ob.a);
            //Console.WriteLine("b = " + ob.b);
            Console.WriteLine("c = " + ob.c);
            //Console.WriteLine("d = " + ob.d);
            //Console.WriteLine("e = " + ob.e);
            //Console.WriteLine("f = " + f);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Derived2 o1 = new Derived2();
            NonDerived1 o2 = new NonDerived1(); 
        }
    }
}

Output

The following output shows that the derived class in another assembly can access only public, protected, and protected internal members of the base class. Also, the only public member is available in the non-derived class of another assembly as shown below.

Demonstrating Access Modifiers in C#
Demonstrating Access Modifiers in C#

Summary

In this post on Access Modifiers in C#, I have discussed each of the six types of access modifiers available in C#. Basically, these access modifiers are private, private protected, internal, protected, protected internal, and public. While the private modifier is most restrictive, the public is least restrictive. Further, this article illustrates the usage of all of these access modifiers with the help of two programs. As shown above, the first program describes the accessibility in the current assembly, whereas the second one describes the accessibility of data members in another assembly.


programmingempire

You may also like...