C# access modifier for exposing class only within namespace

前端 未结 3 347
情书的邮戳
情书的邮戳 2020-12-18 17:46

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

相关标签:
3条回答
  • 2020-12-18 18:06

    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.

    0 讨论(0)
  • 2020-12-18 18:14

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

    0 讨论(0)
  • 2020-12-18 18:15

    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.

    0 讨论(0)
提交回复
热议问题