C# 中命名空间的使用
我想知道 C# 和其他编程语言中命名空间的目的是什么...
据我所知,它们有两个用途:
- 将项目构造成有意义的部分
- 区分具有相同名称的类
我的问题是:使用命名空间时还需要考虑其他事项吗?它们对性能或类似的东西有影响吗?
I was wondering, what the purpose of Namespaces in C# and other programming languages is...
As far as I know, they are used for two things:
- To structure the project into meaningful pieces
- To distinguish classes with the same name
My Question is: Are there any other things to consider when using namespaces? Do they have an impact on performance or something like that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
基本上就是这样。我想补充一点,命名空间提供的结构大于项目的结构,因为命名空间可能跨越项目和程序集。我想补充你的第二点,命名空间的主要目的是为库添加结构以便更容易找到你需要的东西并避免你不需要的东西 。也就是说,名称空间的存在是为了方便库的用户,而不是为了方便其创建者。
次要目的是消除名称冲突的歧义。实际上,名称冲突非常罕见。 (如果命名空间的主要目的是消除冲突,那么可以想象基类库中的命名空间会少得多!)
是的。正确使用命名空间有很多方面。例如:
有关更多详细信息,请参阅我关于此主题的文章:
http://blogs.msdn。 com/b/ericlippert/archive/tags/namespaces/
另请参阅框架设计指南,了解有关命名空间使用的正确和不正确约定的更多想法。
几乎从来没有。命名空间是 C# 语言的虚构;底层类型系统没有“命名空间”。当你说
没有名为“Exception”的类时。类名称是“System.Exception”——名称中包含一个句点。 CLR、反射和 C# 语言共同让您相信该类被命名为“Exception”并且位于命名空间“System”中,但一旦您深入幕后,实际上并不存在命名空间这样的野兽。这只是一个惯例,有时您可以省略“系统”。来自名称“System.Exception”。
That's basically it. I would add to your first point that namespaces provide structure larger than just that of the project, since namespaces may span projects and assemblies. I would add to your second point that the primary purpose of namespaces is to add structure to libraries so that it becomes easier to find stuff you need and avoid stuff you do not need. That is, namespaces are there as a convenience for the user of a library, not for the convenience of its creators.
A secondary purpose is to disambiguate name collisions. Name collisions are in practice quite rare. (If the primary purpose of namespaces was to disambiguate collisions then one imagines there would be a lot fewer namespaces in the base class libraries!)
Yes. There are numerous aspects to correct usage of namespaces. For example:
See my articles on this subject for more details:
http://blogs.msdn.com/b/ericlippert/archive/tags/namespaces/
And see also the Framework Design Guidelines for more thoughts on correct and incorrect conventions for namespace usage.
Almost never. Namespaces are a fiction of the C# language; the underlying type system does not have "namespaces". When you say
there is no class named "Exception". The class name is "System.Exception" -- the name has a period in it. The CLR, reflection, and the C# language all conspire to make you believe that the class is named "Exception" and it is in the namespace "System", but really there is no such beast as a namespace once you get behind the scenes. It's just a convention that you can sometimes omit the "System." from the name "System.Exception".
根据 MSDN 命名空间具有以下属性:
global::System
将始终引用 .NET Framework 命名空间System
。其次,命名空间与性能无关,但如果您创建了自己的命名空间,那么您应该遵循整个项目的约定。
According to MSDN a namespace has the following properties:
global::System
will always refer to the .NET Framework namespaceSystem
.Secondly namespace has nothing to do with performance but if you have created your own namespace so you should follow the conventions across the project.
它不会影响性能。但为了代码的可读性,我建议删除不需要的 using 语句
It doesn't affect performance. But for code readability, I would recommended remove unwanted using statements
命名空间是从早期技术(如 XML)中提取出来的概念。命名空间为您的类提供上下文,允许您在域和数据代码中拥有一个 CUstomer 对象。
您还可以使用命名空间来别名,这仍然执行上述操作,但允许对特定对象进行更短的命名。
域.客户
相对
数据.客户
Namespaces are a concept pulled from earlier technology, like XML. THe namespace gives context to your classes, allowing you to have say a CUstomer object in your domain and in your data code.
You can also use namespaces to alias, which still does the above, but allows shorter naming for the particular object.
domain.customer
versus
data.customer
您已经谈到了两个主要原因。这是来自 MSDN 的旧文章,但它仍然适用: 命名空间命名指南
在Java 世界中,命名实践是反转拥有该产品的公司的域名,并在其后包含该产品的名称。因此 com.example.product 可能是一个有效的命名空间,但您在 .NET 中并没有真正看到它。
You've touched upon the two main reasons. This is an old article from MSDN but it still applies: Namespace Naming Guidelines
In the Java world the naming practice is to reverse the domain name of the company who owns the product and include the product's name after that. So com.example.product might be a valid namespace, but you don't really see that in .NET so much.
这些都是那里的大人物。
并没有真正的性能优势。至少,不是直接的。如果没有命名空间框架,则必须搜索更多位置才能找到您尝试包含的代码 - 这几乎就像需要为每个项目加载整个 .NET 框架一样。嗯,不是真的,但对于这个讨论来说已经足够接近了。
Those are the big ones right there.
There aren't really performance benefits. At least, not directly. without namespaces framework would have to search the a lot more places to find the code you are trying to include - It would almost be like needing to load up the ENTIRE .NET framework for every project. Well, not really, but its close enough for this discussion.