Access Modifiers In C#

Loading

Access Modifiers can be used in everything defined in C#. These are class, struct, function, method, property, and all variables at the class level are used.

So, why do we use access Modifiers?

Because we want to Access Modifiers the accessibility of the definitions we have in our application from outside the code area where they are located.

C# provides five types of access Modifiers.

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

Short table of access Modifiers

1- Private

Private Access Modifiers is used to specify private accessibility to the variable or function. It is most restrictive and accessible only within the body of class in which it is declared.

2- Public

The data accessible defined as public is completely accessible inside and outside the code block. So, there are no restrictions.

Output:

First Name => Robert
Last Name => White

3-Protected

It is accessible within the class. It is also accessible within subclass or child class, in case of inheritance.

Now here, we are accessing protected members within class by inheritance

Output:

First Name => Robert
Last Name => White

4-Internal

an element defined as internal is accessible in the assembly (Dll or Exe file) in which it is located. There are no restrictions on access inside the dll or Exe file, but it cannot be accessed from the outside.

Output:

First Name => Robert
Last Name => White

5-Protected internal

an element defined as protected internally can be accessed inside the class in which it is defined and inside other classes inheritance from that class. It is also accessible in other classes inheritance from the class it is defined in, even if they are not in the same assembly.

Output:

First Name => Robert
Last Name => White

Bir yanıt yazın

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir

Mesut Yiğit

Kaliteli bilgilerin bulunduğu, paylaşımcı ve her daim geliştirici bir öğrenimin benimsendiği Teknik ve diğer konularda kendime ve geleceğe notlar