What are different types of access modifiers available in C#?

CsharpServer Side ProgrammingProgramming

Access modifiers are used to specify the scope of accessibility of a member of a class or type of the class itself. There are six different types of access modifiers.

  • Public

  • Private

  • Protected

  • Internal

  • Protected Internal

  • Private Protected

Public Access Modifier

Objects that implement public access modifiers are accessible from everywhere in a project without any restrictions.

Example

using System;
namespace MyApplication{
   public class Program{
      public static void Main(){
         Person person = new Person();
         Console.WriteLine(person.Name);
         //Person Name is accessible as it is public
      }
   }
   public class Person{
      public string Name = "Mark";
   }
}

Private Access Modifier

Objects that implement private access modifier are accessible only inside a class or a structure. As a result, we can’t access them outside the class they are created.

Example

using System;
namespace MyApplication{
   public class Program{
      public static void Main(){
         Person person = new Person();
         Console.WriteLine(person.Name);
         //Since Name is private it is not accessible in Program class.
         // Error: Person.Name is inaccessible due to its protection level.
      }
   }
   public class Person{
      private string Name = "Mark";
   }
}

Protected Access Modifier

The protected keyword implies that the object is accessible inside the class and in all classes that derive from that class.

Example

using System;
namespace MyApplication{
   public class Program{
      public static void Main(){
         Employee employee = new Employee();
         employee.Print(); //Output: Mark
         Person person = new Person();
         Console.WriteLine(person.Name);
         // Error: Person.Name is inaccessible due to its protection level.
      }
   }
   public class Person{
      protected string Name = "Mark";
   }
   public class Employee : Person{
      public void Print(){
         Console.WriteLine(Name);
      }
   }
}

Internal Access Modifier

For Internal keyword, the access is limited exclusively to classes defined within the current project assembly.

Example

Project 1

 Live Demo

using System;
namespace MyApplication{
   public class Program{
      public static void Main(){
         Person person = new Person();
         Console.WriteLine(person.Name); //Output: Mark
      }
   }
   public class Person{
      internal string Name = "Mark";
   }
}

Output

Mark

Project 2

using MyApplication;
using System;
namespace Project2{
   public class Project2Class{
      public void Print(){
         Person person = new Person();
         Console.WriteLine(person.Name);
         // Error: Person.Name is inaccessible due to its protection level.
      }
   }
}

Protected Internal Access Modifier −

The protected internal access modifier is a combination of protected and internal. As a result, we can access the protected internal member only in the same assembly or in a derived class in other assemblies.

Example

Project 1

 Live Demo

using System;
namespace MyApplication{
   public class Program{
      public static void Main(){
         Person person = new Person();
         Console.WriteLine(person.Name); //Output: Mark
      }
   }
   public class Person{
      protected internal string Name = "Mark";
   }
}

Output

Mark

Project 2

using MyApplication;
using System;
namespace Project2{
   public class Project2Class : Person{
      public void Print(){
         Console.WriteLine(Name); //Output: Mark
      }
   }
}

Private Protected Access Modifier

The private protected access modifier is a combination of the private and protected keywords. We can access members inside the containing class or in a class that derives from a containing class, but only in the same assembly(project). Therefore, if we try to access it from another assembly, we will get an error. This modifier is valid in C# version 7.2 and later.

raja
Published on 04-Aug-2020 10:13:17
Advertisements