Access Modifier In C#

We have a very vital role of Access modifier in C#, there are five different types which are as follows,

  1.  Public 

    Whenever you have to make your member/variables, methods, or properties  available for your entire application, then you can go with Public as an access modifier. It is visible to all other classes without inhertance, for example - HR of an organization has all access, which means HR is available to any department without any constraint.
    1. public class HRDept  
    2.  {  
    3.    public string HR= "Your HR";  
    4.  }  
    5.   
    6. public class Company  
    7.  {   
    8.    public static void Main()  
    9.   {  
    10.     Company com = new Company();  
    11.     Console.WriteLine("Hello World" + " i am " + com.HR);  
    12.    }  
    13. }  
  2. Private

    Its access is to only this class, it cannot be accessible outside class, nor can you  inherit, neither can you create an object of that class. For example - You went to buy ice-cream in an  ice-cream parlour, you will get this only inside the  ice cream parlour, if you want it outside that parlour you won't buy it.
    1. Private class IceCreamParlour  
    2. {  
    3.    private string buttorChoclate;  
    4.  }  
    5. public class IceCream  
    6. {   
    7.   public static void Main()  
    8.   {  
    9.     //throw an compile time exception, inaccessible due to its protection level  
    10.     IceCreamParlour ice= new IceCreamParlour();  
    11.     }  
    12. }  
  3. Protected

    It is accessible outside your class, only to your derived class, if you try to achieve this by creating an object of that class then you won't achieve it.

  4. Internal

    It is accessible only to single assembly/Project, you cannot access member of internal one outside that assembly.

  5. Protected Internal

    It's a combination of Protected and Internal, it is accessible to your derived class and only accessible to your assembly/Project.
Next Recommended Reading Access Modifier In C# With Example