The importance of learning the basics: Access Modifiers

What they are

Luiz Felipe
2 min readFeb 11, 2022

--

What are modifiers?

Well simply saying. Modifiers are keywords in C# which are used to restrict objects, classes, methods, and their members into the program or application.

It is especially used to control the scope of the class and its objects, it provides more security and readability to our application. There are many modifiers, but just two of them are possible to use to declare a class, Public, Internal, and Static. The other modifiers including (Public, Internal, and Static) are used to declare members of the class.

Right down below we can see the most popular types of modifiers in C# and I will introduce each one of them briefly.

  1. Public
  2. Internal
  3. Static
  4. Private
  5. Protected
  6. Protected Internal

Public Access Modifier

The public modifier is the most common access specifier in C#. It can be accessed from anywhere in the application, which means that there is no restriction to be accessed. The scope of the accessibility is inside the class as well as outside. It can be accessed by any other code in the same assembly or another assembly that references it.

Internal Access Modifier

The internal access modifier can be accessed just within the same assembly level but not from another assembly.

Static Access Modifier

The static access modifier can be used by methods, objects, and classes. But, if you declare a static class all the members inside the class must be static as well. Also, static classes cannot be instantiated, the methods are accessed by other assemblies directly.

Key points:

When a namespace has static classes declared, the CLR(Common Language Runtime) loads every static class automatically.

Private Access Modifier

The private access modifier also is one of the most common access specifiers in C#. The private class can only be accessed by the classes or struct in which they are declared. About the member of a private class, they cannot be accessed outside the class, it is limited to use just inside the class, usually for a unique purpose.

Protected Access Modifier

The protected can just be used by members of a class and has accessibility limited within the class or struct.

Protected Internal Access Modifier

The protected internal is basically the same access levels of both protected and internal. It can be accessed anywhere in the same assembly and in the same class also, and for the classes that are inherited from the class.

Hope you have enjoyed this article :)

--

--