What is access modifier in C#, how many types of modifiers in C# explain with example?

Asked 25-Aug-2021
Viewed 540 times

0

What is an access modifier in C#, how many types of modifiers in C# explain with example?


1 Answer


0

Access Modifier
Access Modifiers in C# are those reserved keywords that are used to define the accessibility or scope of any variables, class, method, constructor, etc. These are also called Visibility Modifiers. Access Modifier restricts the accessibility of any class, variables, methods, constructor, etc. to any other class. Access modifiers in C# are mainly used for Encapsulation.
  1. Public ( accessible to any the class)
  2. Private ( access only within the class )
  3. Protected ( access to all derived the class of same class)
  4. Internal ( access to all classes within his assembly )
  5. Protected Internal ( access to the class same assembly with derived class )

Public Member Accessibility
When a class is defined with a public access modifier, the public access members of that class are equally directly accessible in the same assembly and external assembly. Therefore this type of class can be inherited in any assembly without any restriction.
using System;

class PublicAccess
{
 public int Age;        // public access modifier
}
public class Program
{
 public static void Main()
 {
  PublicAccess pa=new PublicAccess();
  pa.Age=18;
  Console.WriteLine('Age : '+pa.Age);
 }
}
Output
Age : 18
Private Member Accessibility
When a class is defined with a private access modifier, the private access members of that class are neither directly accessible in the derived class of the same assembly nor in the derived class of the external assembly. That is, such members are not directly accessible in any derived class but are always accessible through some public method.
using System;

class PrivateAccess
{
 private int Age; // private access modifier can't access out of class
 public int PersonAge // public property access modifier access the private age variable
 {
  set{
   Age=value;
  }
  get{
   return Age;
  }
 }
}
public class Program
{
 public static void Main()
 {
  PrivateAccess pa=new PrivateAccess();
  pa.PersonAge=18;
  Console.WriteLine('Age : '+pa.PersonAge);
 }
}
Output
Age : 18
Protected Member Accessibility
When a class is defined with a protected access modifier, the members with protected access of that class are directly accessible to Derived Classes in the same Assembly and External Assembly, but for those outside the Derived Class, these members are private members. Generally, all the member methods defined in a class are not needed by the external classes. Rather, many times we define such methods, which are needed only by Derived Class. These types of methods are defined using the protected access modifier. Because Protected Methods are accessible in Derived Class like Public Members, but for other External Classes outside the Class, these Methods are completely Private i.e. Inaccessible.
using System;

public class ProtectedAccess
{
 protected int Age;
 public int PersonAge
 {
  set{
  Age = value ;
  }
  get{
  return Age;
  }
 }
}
public class Program : ProtectedAccess
{
 public static void Main()
 {
  Program pa=new Program();
  pa.PersonAge=18;
  Console.WriteLine('Age : '+pa.Age);
 }
}
Output
Age : 18
Internal Member Accessibility
When a class is defined with a public access modifier, the internal access members of that class are directly accessible to the derived classes in the same assembly, but completely restricted to the external assembly.
using System;

internal class InternalAccess
{
 private int Age; // private access modifier can't access out of class
 public int PersonAge // public property access modifier access the private age variable
 {
  set{
   Age=value;
  }
  get{
   return Age;
  }
 }
}
public class Program
{
 public static void Main()
 {
  InternalAccess pa=new InternalAccess();
  pa.PersonAge=18;
  Console.WriteLine('Age : '+pa.PersonAge);
 }
}
Output
Age : 18
Protected Internal Member Accessibility
When a class is defined with a public access modifier, members with protected internal access to that class are directly accessible to derived classes in the same assembly, but completely restricted to the external assembly.
using System;

public class InternalProtected Access
{
 protected internal int Age;
}
public class Program : InternalProtectedAccess
{
 public static void Main()
 {
  Program pr=new Program();
  pr.Age=18;
  Console.WriteLine('Age : '+pr.Age);
 }
}
Output
Age : 18