Jaco Jaco - 10 months ago 64
C# Question

C# access modifier for exposing class only within namespace

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?

Answer Source

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.