Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Practical uses for the "internal" keyword in C#

Could you please explain what the practical usage is for the internal keyword in C#?

I know that the internal modifier limits access to the current assembly, but when and in which circumstance should I use it?

like image 921
Alexander Prokofyev Avatar asked Oct 03 '08 04:10

Alexander Prokofyev


People also ask

What is internal used for?

Internal, in C#, is a keyword used to declare the accessibility of a type or type member such that the access is limited to the assembly in which it is declared. An internal modifier is used to prevent the use of a public modifier, which allows access to other assemblies wherever necessary.

What is the use of internal class?

Internal access specifier allows a class to expose its member variables and member functions to other functions and objects in the current assembly. Any member with internal access specifier can be accessed from any class or method defined within the application in which the member is defined.

What is use of internal keyword in C#?

The internal keyword is an access modifier for types and type members. This page covers internal access. The internal keyword is also part of the protected internal access modifier. Internal types or members are accessible only within files in the same assembly, as in this example: C# Copy.

How do you use an internal access modifier?

internal modifier The internal keyword is an access modifier for types and type members. We can declare a class as internal or its member as internal. Internal members are accessible only within files in the same assembly (. dll).


2 Answers

Utility or helper classes/methods that you would like to access from many other classes within the same assembly, but that you want to ensure code in other assemblies can't access.

From MSDN (via archive.org):

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

You can also use the internal modifier along with the InternalsVisibleTo assembly level attribute to create "friend" assemblies that are granted special access to the target assembly internal classes.

This can be useful for creation of unit testing assemblies that are then allowed to call internal members of the assembly to be tested. Of course no other assemblies are granted this level of access, so when you release your system, encapsulation is maintained.

like image 113
Ash Avatar answered Oct 22 '22 18:10

Ash


If Bob needs BigImportantClass then Bob needs to get the people who own project A to sign up to guarantee that BigImportantClass will be written to meet his needs, tested to ensure that it meets his needs, is documented as meeting his needs, and that a process will be put in place to ensure that it will never be changed so as to no longer meet his needs.

If a class is internal then it doesn't have to go through that process, which saves budget for Project A that they can spend on other things.

The point of internal is not that it makes life difficult for Bob. It's that it allows you to control what expensive promises Project A is making about features, lifetime, compatibility, and so on.

like image 45
Eric Lippert Avatar answered Oct 22 '22 19:10

Eric Lippert