Search⌘ K
AI Features

Access Modifiers

Explore how to use access modifiers in C# to control the visibility and scope of classes and their members. Understand public, private, protected, internal, protected internal, and private protected modifiers to organize and protect your code effectively within and across assemblies.

All class members have access modifiers. We already encountered them when we decorated our classes with the public keyword.

Access modifiers control the visibility and scope of classes and their members.

  • The public modifier makes a class or member visible to all external code. Any part of the program, including code in different assemblies, can access them.

  • The private modifier restricts access to the containing class. Private members are accessible only from within the class where they are defined.

  • The protected modifier can only be used with class members. These members are only accessible from the same class or any derived class.

  • The internal modifier ensures classes and members are only visible inside the assembly where they are defined.

Note: An assembly is the compiled output of our code. In modern .NET, one project usually results in one assembly (a .dll file). The internal modifier ensures code is only visible to other classes within that same project.

  • The protected internal modifier indicates that classes and members are accessible ...