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?
.net-assemblyaccess-modifierscinternals
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?
Best Answer
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):
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.