C#. Encapsulation in classes. Keywords private, protected, public, internal




Encapsulation in classes. Keywords private, protected, public, internal


Contents


Search other websites:

1. Categories of access modifiers

When using classes in the C# language, access modifiers are used, which fall into two categories:

  • modifiers that define access to the entire class in the assembly and outside of it;
  • modifiers that determine access to a member (element) of the class.

 

2. Access modifiers for a class

If a class is declared in the program, then this class can have one of two access modifiers:

  • public – in this case, the class is considered public. It is available to any code in any assembly;
  • internal – in this case, the class is considered available only in the assembly where it is defined.

The access modifier for a class is placed before the class keyword:

access_modifier class ClassName
{
  // ...
}

here

  • ClassName – class name;
  • access_modifier – one of the access modifiers: public or internal.

A class can be declared without specifying an access modifier. In this case, the default is the internal modifier for the class.

For example.

// A class that is available outside the assembly
public class MyPublicClass
{
  // ...
}

// A class that is only available within the current assembly
internal class MyInternalClass
{
  // ...
}

 

3. Access modifiers for an element (member) of a class

If an element (method, data member, etc.) is declared in a class, then an access modifier is indicated before it. This modifier determines the level of restriction of access to the class member. The C# language offers the following access modifiers:

  • private;
  • protected;
  • public;
  • internal;
  • protected internal.

The protected and protected internal access modifiers are used in inheritance.

A class member can also be declared without a modifier. In this case, the private level of access to the class member is assumed.

 

4. The private keyword. Features of use

If an element (method, data member, reference, etc.) with the private access modifier is declared in a class, then the following access rules apply to it:

  • the element is inaccessible to any instances (objects) of the class (Figure 1);
  • the element is inaccessible to any inherited classes (Figure 2);
  • the element is available to the methods of the class in which it is declared (Figure 3).

C#. Access modifier private. No access from the class instance to the private element

Figure 1. Access modifier private. No access from the class instance to the private element value

 

C#. Access modifier private. No access from the inherited class to the private-element value of the class

Figure 2. Access modifier private. No access from the inherited class to the private-element value of the class

 

C#. Access modifier private. Access from internal methods of a class to the private-element

Figure 3. Access modifier private. Access from internal methods of a class to the private-element

 

5. The public keyword. Figure

When declaring a class member with the public keyword, the following rules apply:

  • the element is considered available to all methods of the current assembly (Figures 4, 5);
  • an element is accessible from methods of external assemblies if the element’s class is declared as public. If a class with a public element is declared as internal, then this element is not accessible from methods of external assemblies.

 

C#. Access modifier public. Access from a class instance to the public-member of the class

Figure 4. Access modifier public. Access from a class instance to the public-member value of the class

C#. Access modifier public. Access from an inherited class

Figure 5. Access modifier public. Access from an inherited class

 

6. The keyword protected. Figure

If an element (method, data member, etc.) with the protected modifier is declared in a class, then the following access restrictions apply:

  • the element is inaccessible from the class instance (Figure 6);
  • the element is accessible from methods and nested classes implemented in the current class;
  • the element is accessible from the methods of the inherited class (Figure 7).

 

C#. Access modifier protected. No access from class instance

Figure 6. Access modifier protected. No access from class instance

C#. Access modifier protected. Access from an inherited class

Figure 7. Access modifier protected. Access from an inherited class

 

7. The internal keyword. Figure

If an element (method, data member, etc.) of a class is declared with the internal keyword, then the following access rules apply:

  • the class element is accessible from all methods of the current assembly. In the current assembly, the action of the internal modifier is the same as the action of the public modifier (see Figures 4, 5);
  • the class element is inaccessible from the methods of other assemblies (Figure 8).

 

C#. Access modifier internal. No access from another assembly

Figure 8. Access modifier internal. No access from another assembly

 

8. Access modifier protected internal. Figure

If an element with the protected internal access modifier is declared in a class, then the following rules apply:

  • the class element is accessible from all methods of the current assembly. The internal modifier extension is in effect here (see figure 9);
  • a class element is accessible from methods of inherited classes regardless of assembly (Figure 9). The protected modifier extension works here. Classes can be inherited in other assemblies;
  • the element of the class is not available from the methods of other assemblies (class instances). The restriction of the internal modifier is in effect here (Figure 10).

 

C#. Access modifier protected internal. Access to the variable of the method of inherited class of other assembly

Figure 9. Access to the variable value of the method inherited class B other assembly App2

 

C#. Access modifier protected internal. Value cannot be accessed from a class instance of another assembly

Figure 10. Value cannot be accessed from a class instance of another assembly

Of course, in order to access the App1 assembly from the App2 assembly, you must first include this assembly using Microsoft Visual Studio tools. But this is a completely different topic…

 


Related topics