C# Access Modifiers/Specifiers

Access modifiers or specifiers in C# define the accessibility of the member, class, or datatype in a program. These are the keyword that restricts unwanted manipulation of data in a program by other classes.

There are five types of access specifiers in C#.

  1. Public
  2. Protected
  3. Internal
  4. Protected internal
  5. Private

Let us go through each of them with an example.

Public Access Specifier

Declaring fields and methods or classes public means making it accessible everywhere in a program or in an application. It is accessed by the entire program. Any public member can be accessed from outside the class.

Example: C# program to demonstrate public access specifier.

using System;

namespace Program
{
  class Student
  {
    public string name;
    public int age;

    public Student(String n, int a)
    {
      name = n;
      age = a;
    }

    public void print()
    {
      Console.WriteLine("Name: {0}", name);
      Console.WriteLine("Age: {0}", age);
    }
  }

  class PublicAccess
  {
    static void Main(string[] args)
    {
      Student obj = new Student("John Mac", 15);

      // Accessing public method  
      obj.print();

      // Accessing public variable name 
      Console.WriteLine("My name is: {0}", obj.name);

    }
  }
}

Output:

Name: John Mac
Age: 15
My name is: John Mac


Protected Access Specifier

Declaring fields and methods protected means limiting its accessibility only to its class and derived types of this class. In the case of inheritance, the sub-class can access the private member of the derived class.

Example: C# program to demonstrate protected access specifier.

using System;

namespace protectedAccess
{
  class Animal
  {
    protected String str = "Animals";

    protected void eat()
    {
      Console.WriteLine(str + " eat.");
    }
  }

  // Dog class inherits Animals Class
  class Dog: Animal
  {
    public void sound()
    {
      eat();  //Accessing protected member
      Console.WriteLine("Barking...");
    }
  }

  class Program
  {
    static void Main(string[] args)
    {
      Dog obj = new Dog();

      // Accessing through the child class
      obj.sound();
    }
  }
}

Output:

Animals eat.
Barking…


Internal Access Specifier

Declaring fields, methods, or class internal means limiting its accessibility only within files in the current assembly. It is a default specifier in the C# program, internal members can be accessed anywhere within its namespace.

Example: C# program to demonstrate internal access specifier.

using System;

namespace Program
{
  class Student
  {
    internal string name = "Jason Marsh";

    internal void print(int age)
    {
      Console.WriteLine("Age: {0}", age);
    }
  }

  class PublicAccess
  {
    static void Main(string[] args)
    {
      Student obj = new Student();

      // Accessing internal variable name 
      Console.WriteLine("My name is: {0}", obj.name);

      // Accessing internal method  
      obj.print(16);

    }
  }
}

Output:

My name is: Jason Marsh
Age: 16


Protected Internal Access Specifier

Declaring fields or methods protected internal means limiting its accessibility to the current assembly and within a derived class in another assembly.

Example: C# program to demonstrate protected internal access specifier.

using System;

namespace Program
{
  class Student
  {
    protected internal string name = "Jason Marsh";

    protected internal void print(int age)
    {
      Console.WriteLine("Name: {0}", age);
    }
  }

  class PublicAccess
  {
    static void Main(string[] args)
    {
      Student obj = new Student();

      // Accessing internal variable name 
      Console.WriteLine("My name is: {0}", obj.name);

      // Accessing internal method  
      obj.print(16);

    }
  }
}

Output:

My name is: Jason Marsh
Age: 16


Private Access Specifier

It is specified using the private keyword. Variables, Methods, and constructors that are declared private modifiers are only accessed within the declared class itself. It is the most access restricting specifier among all. This specifier is mostly used when you want to hide your members.

It is used to achieve encapsulation in C#. If you want to access the data, we use the set and get method.

Example: C# program to demonstrate private access specifier.

using System;

public class Student
{
  // private variables
  private String name;
  private int roll;

  // using setter and getter for name
  public String stdName
  {
    get
    {
      return name;
    }

    set
    {
      name = value;
    }
  }

  // using setter and getter for roll
  public int stdRoll
  {
    get
    {
      return roll;
    }

    set
    {
      roll = value;
    }
  }
}

class StudentInfo
{
  static public void Main()
  {
    // creating object
    Student obj = new Student();

    //setting the values
    obj.stdName = "John Mac";
    obj.stdRoll = 15;

    // Getting the values
    Console.WriteLine("Student Name: " + obj.stdName);
    Console.WriteLine("Studen Roll: " + obj.stdRoll);
  }
}

Output:

Student Name: John Mac
Studen Roll: 15