Access modifier "Internal" What is it for? C #

1

What is the use of this modifier? It is not entirely clear to me when you let Visual Studio create methods for you (for example, writing a method that does not exist in an object and that IntelliSense implements the method for you ...) Always (or almost always) the method with access modifier Internal , instead of public, private or protected ...

Why does he do it? What is the point of the Internal?

    
asked by Edulon 04.04.2018 в 19:20
source

4 answers

1

Summary:

internal is for the scope of the assembly (that is, only accessible from the code in the same .exe or .dll). Internal members are only accessible within the assembly by inheritance (its derived type) or by class instance.

Reference:

dotnetbull: what is the access modifier in C # (Info in English)

Expanded:

Classes / utility methods or helpers that you would like to access from many other classes within the same assembly, but to which you want to make sure that the code in other assemblies can not access.

From the official reference C # :

  

A common use of internal access is in component-based development because it allows a group of components to cooperate privately without being exposed to the rest of the application code. For example, a framework for building graphical user interfaces could provide Control classes and Forms that cooperate using members with internal access. Because these members are internal, they are not exposed to the code that the framework uses.

You can also use the internal switch together with the InternalsVisibleTo of assembly level to create "friend" assemblies that are given special access to the inner classes of the target assembly.

This can be useful for the creation of unit test sets that can then call the internal members of the set to be tested. Of course, no other assembly is granted this level of access, so when it releases its system, the encapsulation is maintained.

    
answered by 04.04.2018 / 20:18
source
1

The internal modifier makes the methods and properties defined in this way accessible to elements that are compiled in the same binary, regardless of the environment.
That is, if you want to make a framework or a library and its elements are accessed more easily declare them with internal, so that within your framework can be accessed, but when someone else wants to use them with your assembly compiled as a reference I can not access them and only see the methods that I have exposed.

    
answered by 04.04.2018 в 19:27
1

Taken from Microsoft Docs

  

Only internal types or members can be accessed from files in the same assembly.

     

A common use of internal access occurs in component-based development because it allows a group of components to cooperate privately without being exposed to the rest of the application code. For example, a framework for creating graphical user interfaces could provide the Control and Form classes that cooperate through members with internal access. Because these members are internal, they are not exposed to the code that the framework uses.

Basically an internal method is public but only within the assembly that contains it.

I do not want to be redundant with the documentation. Conceptually, what you do with the internals methods is to make a public method within the library but with the difference that you do not want to expose it as part of the API of the library.

A good practice (and that's why Visual Studio does it) is to make the methods be done first internal and then, once you decide which will be the API of your application, start exposing them as public .

    
answered by 04.04.2018 в 19:30
0

It is related to programacion orientada a objetos , maybe the official doc provides some clarity with examples

internal (C # Reference)

basicly define how the accessibility of the class will be, for example if you defend in a library a class as internal you can only use within that library and not from another that makes reference

Accessibility levels (C # Reference)

in the link mentions

  

internal: Access is limited to the current assembly.

    
answered by 04.04.2018 в 19:57