C# access modifier for exposing class only within namespace

C#.NetNamespaces

C# Problem Overview


In java you have package level protection that ensures classes are only usable within the package.

Namespaces in C# act more or less like packages. But C# does not have a protection level for protecting classes within a namespace.

Is there a specific reason for this?

C# Solutions


Solution 1 - C#

There is no such access modifier: the closest modifier is internal, but the unit of protection is the assembly in which the class resides, not its namespace.

One could argue that it is possible to achieve similar level of control using internal, because both kinds of restriction keep outsiders from accessing the implementation details of your library. The only person to whom it makes a difference is you, the writer of the library, and you are in full control of what to expose and what to hide anyway. Essentially, it means that if you do not want to use a class outside its namespace, simply refrain from using it; if the class is internal, nobody else will be able to use that class either.

Solution 2 - C#

In .NET there are assemlies(dll or exe files), you can use internal modifier to limit access only within the same assembly

Solution 3 - C#

> Is there a specific reason for this?

Mostly, it's because there are some key differences between packages and namespaces

To simplify what's already been said in the linked question and here: Namespaces in C# are mostly to help with organizing an assembly's contents, both internally and externally. Java packages have more in common with C# assemblies, and there is an access modifier in C# that restricts to the assembly level: internal.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJacoView Question on Stackoverflow
Solution 1 - C#Sergey KalinichenkoView Answer on Stackoverflow
Solution 2 - C#Alexandr MihalciucView Answer on Stackoverflow
Solution 3 - C#UserView Answer on Stackoverflow