DEV Community

Cover image for C# Access Modifiers
Zafar Urakov
Zafar Urakov

Posted on

C# Access Modifiers

Well, since we started talking about classes in C#, we can't escape access modifiers.

What is access modifiers?

In C#, access modifiers specify the accessibility of types (classes, interfaces, etc) and type members (fields, methods, etc). For example:

class Student
{
   private int age = 12;
   public string name = "Bob";
}
Enter fullscreen mode Exit fullscreen mode

age - private field can only be accessed within the Student class
name - public field that can be accessed from anywhere.

Types of Access Modifiers

In C#, there are 4 basic types of access modifiers.

  • public
  • private
  • protected
  • internal

1. Public Access Modifier

When we declare a type or type member public, it can be accessed from anywhere. For example:

using System;

namespace AccessModifiers 
{
  class Student 
  {
    public string name = "Bob";

    public void SayHello() =>
      Console.WriteLine("Hello from Student class");
  }

  class Program 
  {
    static void Main(string[] args) 
    {

      // creating object of Student class
      Student student = new Student();

      // accessing name field and printing it
      Console.WriteLine("Name: " + student.name);

      // accessing SayHello method from Student
      student.SayHello();
     }
  }
}
Enter fullscreen mode Exit fullscreen mode

Output

Name: Bob
Hello from Student class
Enter fullscreen mode Exit fullscreen mode

In the above example, we have created a class named Student with a field name and a method SayHello().

      // accessing name field and printing it
      Console.WriteLine("Name: " + student.name);

      // accessing SayHello method from Student
      student.SayHello();
Enter fullscreen mode Exit fullscreen mode

Since the field and method are public, we are able to access them from the Program class.

Note: We have used the object student of the Student class to access its members. To learn more, visit the C# class and objects.

2. Private Access Modifier

When we declare a type member with the private access modifier, it can only be accessed within the same class or struct. For example:

using System;

namespace AccessModifiers 
{
  class Student 
  {
    private string name = "Bob";

    private void SayHello() =>
      Console.WriteLine("Hello from Student class");
  }

  class Program 
  {
    static void Main(string[] args) 
    {

      // creating object of Student class
      Student student = new Student();

      // accessing name field and printing it
      Console.WriteLine("Name: " + student.name);

      // accessing SayHello method from Student
      student.SayHello();
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

In the above example, we have created a class named Student with a field name and a method SayHello().

      // accessing name field and printing it
      Console.WriteLine("Name: " + student.name);

      // accessing SayHello method from Student
      student.SayHello();
Enter fullscreen mode Exit fullscreen mode

Since the field and method are private, we are not able to access them from the Program class. Here, the code will generate the following error.

Error    CS0122    'Student.name' is inaccessible due to its protection level    
Error    CS0122    'Student.SayHello()' is inaccessible due to its protection level
Enter fullscreen mode Exit fullscreen mode

3.Protected Access Modifier

When we declare a type member as protected, it can only be accessed from the same class and its derived classes. For example:

using System;

namespace AccessModifiers 
{
  class Student 
  {
    protected string name = "Bob";
  }

  class Program 
  {
    static void Main(string[] args) 
    {
      // creating object of student class
      Student student = new Student();

      // accessing name field and printing it
      Console.WriteLine("Name: " + student.name);
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

In the above example, we have created a class named Student with a field name. Since the field is protected, we are not able to access it from the Program class.

Here, the code will generate the following error:

Error    CS0122    'Student.name' is inaccessible due to its protection level
Enter fullscreen mode Exit fullscreen mode

Now, let's try to access the protected member from a derived class:

using System;

namespace AccessModifiers 
{
  class Student 
  {
    protected string name = "Bob";
  }

  // derived class
  class Program : Student 
  {
    static void Main(string[] args) 
    {

      // creating object of derived class
      Program program = new Program();

      // accessing name field and printing it
      Console.WriteLine("Name: " + program.name);
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Output

Name: Bob
Enter fullscreen mode Exit fullscreen mode

In the above example, we have created a class Student with a protected field name. Notice that we have inherited the Program class from the Student class.

// accessing name field and printing it
      Console.WriteLine("Name: " + program.name);
    }
Enter fullscreen mode Exit fullscreen mode

Since the protected member can be accessed from derived classes, we are able to access name from the Program class.

4.Internal Access Modifier

When we declare a type or type member as internal, it can be accessed only within the same assembly, that is, we cannot call internal from another project in our Solutions.

An assembly is a collection of types (classes, interfaces, etc) and resources (data). They are built to work together and form a logical unit of functionality.

That's why when we run an assembly all classes and interfaces inside the assembly run together.

Call in our project. For example:

using System;

namespace AccessModifiers 
{
  class Student 
  {
   internal string name = "Bob";
  }

  class Program 
  {
    static void Main(string[] args) 
    {
      // creating object of Student class
      Student student = new Student();

      // accessing name field and printing it
      Console.WriteLine("Name: " + student.name);
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Output

Name: Bob
Enter fullscreen mode Exit fullscreen mode

Call in another project. For example:

using System;

namespace SecondAccessModifiers 
{
  class Program 
  {
    static void Main(string[] args) 
    {
      // creating object of Student class
      Student student = new Student();

      // accessing name field and printing it
      Console.WriteLine("Name: " + student.name);
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Note: Pay attention to the namespace, it is different from the first project.
Output

Error    CS0122    'student.name' is inaccessible due to its protection level
Enter fullscreen mode Exit fullscreen mode

Conclusion

In general, access modifiers allow you to specify the allowed scope for class components. That is, access modifiers determine the context in which a given variable or method can be used.

I have a lot of interesting code on GitHub

Top comments (0)