Access Modifiers in C#

0
321

Access modifiers in C# are keywords which define the accessibility of type or other members like methods, class fields etc. That means it decides which part of the program can access the members.

Access modifiers are also known as ‘access specifiers’.  They play very important role to secure the data. Every type or members like method or variable has some modifier. If no modifier is given then default modifier is applied.

Access Specifier C#

C# provides following types of access modifiers:

  1. Private:

The private access modifier is least access modifier in C#. The members declared as private are accessible only within the class or struct in which they are declared. If in class no access modifier is given to class members then they are private by default.

Example:

using System;
namespace AccessModifiers
{
   class PrivateAccess
   {
      private int val;
      public void Show()
      {
         val = 30;//Private val accessible here
         Console.WriteLine(val);
      }
   }
   class Program
   {
      static void Main(string[] args)
      {
         PrivateAccess PA = new PrivateAccess();
         //Variable val is not accessible here and this line raise error
         //PA.val = 60;
         PA.Show();
         Console.ReadLine();
      }
   }
}

The output of the program is:

30

In above program, the integer variable val is declared as private in class PrivateAccess, so it is accessible to only PrivateClass methods like Show() method. If we try to access this variable in Program class then compiler throws the error. Suppose we remove comments of line

PA.val=60;

Then the compiler raises the error as below:

PrivatAccess

  1. Protected:

The members declared as protected are accessible only within the same class in which they are declared and derived classes of that class. In inheritance process, the protected members of parent class become the protected members of derived class. So in a case of multilevel inheritance if derived class is again inherited by other class then the protected members of the initial base class also inherited in second derived class also. The levels of inheritance do not affect the behavior of protected members.

Recommended Post   C# Methods 

Example:

using System;
namespace ProtectedAccess
{
   class BaseClass
   {
      protected int val;
      int val1;//By default private
   }
   class DerivedClass1 : BaseClass
   {
      public void Show()
      {
         val = 29;//Accessing BaseClass protected variable val
         //val1=10;//Private variable not accessible here
         Console.WriteLine("The value of variable val in first derived class is {0} ", val);
      }
   }
   class DerivedClass2 : DerivedClass1
   {
      public void Display()
      {
         val = 39;//Accessing BaseClass protected variable val
         Console.WriteLine("The value of variable val in second derived class is {0} ", val);
      }
   }
   class Program
   {
      static void Main(string[] args)
      {
         DerivedClass1 DC1 = new DerivedClass1();
         // Protected member val not accessible here
         //DC1.val=100;
         DC1.Show();
         DerivedClass2 DC2 = new DerivedClass2();
         DC2.Display();
         Console.ReadLine();
      }
   }
}

The output of the program is:

The value of variable val in first derived class is 29
The value of variable val in first derived class is 39

In above example, we declare protected integer variable val and private integer variable val1 in class BaseClass. Class DerivedClass1 inherited from BaseClass so the variable val becomes the protected member of this class and we can access it in method Show() but private val1 is not accessible. Class DerivedClass2 is derived from the DerivedClass1, so it accesses the protected variable val which is originally inherited in DerivedClass1 from BaseClass.  As variable val is protected, it is not accessible in class Program. If we remove comment of line

DC1.val=100;

Then the program raises the error as below:

ProtectedAccess

  1. Internal:

Internal modifier makes members and types visible only within the current assembly (project). The internal members are accessible in all classes in the same assembly. The default modifier of the class is internal.

Example:

using System;
namespace InternalModifier
{
   class BaseClass
   {
      internal int val;
   }
   class DerivedClass:BaseClass
   {
      public void Show()
      {
         val = 90;//Accessing internal variable val in derived class
         Console.WriteLine(val);
      }
   }
   class Program
   {
      static void Main(string[] args)
      {
         BaseClass objBaseClass = new BaseClass();
         //Accessing internal variable val in non-derived class
         objBaseClass.val = 120;
         Console.WriteLine(objBaseClass.val);
         DerivedClass derivedClass = new DerivedClass();
         derivedClass.Show();
         Console.ReadLine();
      }
   }
}

The output of the program is:

Recommended Post   C# String Manipulation 

120
90

Here variable val has the internal access modifier, so it is accessible in any class in the current project. We access val in derived class method Show() as well as non derived class Program.

  1. Public:

To make types or members accessible anywhere public access modifier is used in C#. Once a member declared as public you can access them in any class or in any assembly. There is no restriction on accessibility on the public modifier.

  1. Internal Protected:

The modifier internal protected is a combination of internal and protected modifier. The members declared as internal protected behave as internal in current assembly and in other assemblies they become protected. That means in internal protected members are accessible to all classes in current assembly but outside of the current assembly, they are accessible to only derive classes.

Example:

For above two access modifiers, we take one example in which we create two projects one is a class library and other is a console application. We use visual studio to run this programs and following steps shows how to work in visual studio.

  1. First, create project as ‘AccessDemo’ of type class library and following classes in this project
using System;
namespace AccessDemo
{
   public class Demo
   {
      public int number1;
      protected internal int number2;
   }
   public class ClientClass
   {
      public void Show()
      {
         Demo demo1 = new Demo();
         demo1.number1 = 10;//Public variable accessible here
         demo1.number2 = 20;//Protected internal accessible here
         Console.WriteLine("The value of number1 and number2 is {0} and {1}",demo1.number1,demo1.number2);
      }
   }
}
  1. To build this class library select project in solution explorer then right click on it and select build option. After building this project class library (.dll) is generated in projects bin directory.
  2. Right click on the solution in solution explorer and select Add-> new project option. Then provide the name for the new project in this program it is ‘AccessDemoClient’.
  3. Then add reference of the class library in this project by right clicking of references option in solution explorer and selecting Add reference option. From next window browse and add .dll of the previous project.
  4. Write following code in a new project.
using AccessDemo;

using System;
namespace AccessDemoClient
{
   class ClientClass1 : Demo
   {
      public void Display()
      {
         number1 = 100;//Public variable accessible here
         number2 = 200;//Protected internal accessible here
         Console.WriteLine("The value of number1 and number2 is {0} and {1}",number1, number2);
      }
   }
   class Program
   {
      static void Main(string[] args)
      {
         ClientClass clientClass = new ClientClass();
         clientClass.Show();
         ClientClass1 clientClass1 = new ClientClass1();
         clientClass1.Display();
         Demo demo1 = new Demo();
         demo1.number1 =1000;//Public variable accessible here
         //demo1.number2 = 2000;//Protected internal not accessible here
         Console.WriteLine("The value of number1 is {0} ", demo1.number1);
         Console.ReadLine();
      }
   }
}
  1. Right click on the AccessDemoClient project in solution explorer and select option set as start up project. And run project then following output will display.
Recommended Post   C# Type Conversion

The value of number1 and number2 is 10 and 20
The value of number1 and number2 is 100 and 200
The value of number1 is 1000

In this example, variable number1 is public so we can access it in both projects. But the variable number2 is protected internal, so we can access it in any class in project AccessDemo and for AccessClientDemo it is accessible only within inherited class ClientClass1. If we remove the comment of below line from Program class:

demo1.number2=2000;

Then the compiler raises the error like below:

ProtectedInternal

SHARE
Previous articleC# Interface
I am software developer in dot net technology.

LEAVE A REPLY

Please enter your comment!
Please enter your name here