C#访问修饰符,仅在命名空间中公开类 [英] C# access modifier for exposing class only within namespace

查看:152
本文介绍了C#访问修饰符,仅在命名空间中公开类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,您具有程序包级保护,可确保类仅在程序包内可用.

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

C#中的命名空间或多或少像软件包.但是C#没有保护名称空间中的类的保护级别.

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

是否有特定原因?

推荐答案

没有这样的访问修饰符:最接近的修饰符是internal,但是保护的单位是该类所在的程序集,而不是其名称空间.

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.

有人认为使用internal可以实现类似的控制级别,因为两种限制都使局外人无法访问您的库的实现细节.唯一与之不同的人就是您,图书馆的作者,您完全可以控制公开和隐藏的内容.从本质上讲,这意味着如果您不想在其名称空间之外使用某个类,只需避免使用它;如果该类是internal,则其他任何人都将无法使用该类.

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.

这篇关于C#访问修饰符,仅在命名空间中公开类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆