C# 命名空间命名约定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/918894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
namespace naming conventions
提问by Vince Panuccio
For those of you out there writing reusable components, what do you consider to be best practice if you're extending the functionality of the .NET framework?
对于那些编写可重用组件的人,如果您要扩展 .NET 框架的功能,您认为最佳实践是什么?
For example, I'm creating a Pop3 library at the moment as one doesn't exist in .NET. Do I create a custom namespace or do I use System.Net.Mail
?
例如,我目前正在创建一个 Pop3 库,因为 .NET 中不存在该库。我是创建自定义命名空间还是使用System.Net.Mail
?
采纳答案by coobird
From the Namespace Naming Guidelines:
从命名空间命名指南:
The general rule for naming namespaces is to use the company name followed by the technology name and optionally the feature and design as follows. Copy Code
CompanyName.TechnologyName[.Feature][.Design]
命名命名空间的一般规则是使用公司名称后跟技术名称以及可选的功能和设计,如下所示。复制代码
CompanyName.TechnologyName[.Feature][.Design]
Generally it's a really bad practice to start including things into the default namespace of a framework or library. This can cause confusion in terms of whether a new namespace is part of the existing library that is part of a framework that is distributed to everyone, or is part of a custom framework that was added by someone else.
通常,开始将内容包含到框架或库的默认命名空间中是一种非常糟糕的做法。这可能会导致混淆,新命名空间是现有库的一部分,该库是分发给每个人的框架的一部分,还是其他人添加的自定义框架的一部分。
Also, the naming convention tries to avoid namespace collisions by having unique identifiers such as CompanyName
. It also reduces any confusion and issues in terms of the source of the new library.
此外,命名约定试图通过使用诸如CompanyName
. 它还减少了新库来源方面的任何混淆和问题。
This is not only a Microsoft thing, but in the Java as well. Namespaces in Java, called "packages" has the following convention:
这不仅是微软的事情,在 Java 中也是如此。Java 中的命名空间,称为“包”具有以下约定:
The prefix of a unique package name is always written in all-lowercase ASCII letters and should be one of the top-level domain names, currently com, edu, gov, mil, net, org, or one of the English two-letter codes identifying countries as specified in ISO Standard 3166, 1981.
Subsequent components of the package name vary according to an organization's own internal naming conventions. Such conventions might specify that certain directory name components be division, department, project, machine, or login names.
唯一包名的前缀总是用全小写的ASCII字母书写,并且应该是顶级域名之一,目前是com、edu、gov、mil、net、org或英文双字母代码之一识别 ISO 标准 3166, 1981 中规定的国家/地区。
包名称的后续组成部分根据组织自己的内部命名约定而有所不同。此类约定可能指定某些目录名称组件是部门、部门、项目、机器或登录名。
So, if I had a super awesome piece of software, it may be in the net.coobird.superawesomesoftware
package.
所以,如果我有一个超级棒的软件,它可能在net.coobird.superawesomesoftware
包里。
And using package names that contain the default java.
, javax.
, com.sun.
packages are a big no-no.
并且使用包含默认java.
, javax.
,com.sun.
包的包名是一个很大的禁忌。
回答by akjoshi
Also have a look at following MSDN article for guidelines about naming Namespaces
另请查看以下 MSDN 文章,了解有关命名命名空间的指南
The name chosen for a namespace should indicate the functionality made available by types in the namespace. For example, the System.Net.Sockets namespace contains types that enable developers to use sockets to communicate over networks.
The general format for a namespace name is as follows:
<Company>.(<Product>|<Technology>)[.<Feature>][.<Subnamespace>]
For example,
Microsoft.WindowsMobile.DirectX
.
为命名空间选择的名称应指示命名空间中的类型提供的功能。例如,System.Net.Sockets 命名空间包含使开发人员能够使用套接字通过网络进行通信的类型。
命名空间名称的一般格式如下:
<Company>.(<Product>|<Technology>)[.<Feature>][.<Subnamespace>]
例如,
Microsoft.WindowsMobile.DirectX
。