C# 中命名空间的使用

发布于 2024-11-06 01:15:57 字数 161 浏览 1 评论 0原文

我想知道 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

甚是思念 2024-11-13 01:15:57

据我所知,它们有两个用途:

• 将项目构建为有意义的部分

• 区分具有相同名称的类

基本上就是这样。我想补充一点,命名空间提供的结构大于项目的结构,因为命名空间可能跨越项目和程序集。我想补充你的第二点,命名空间的主要目的是为库添加结构以便更容易找到你需要的东西并避免你不需要的东西 。也就是说,名称空间的存在是为了方便库的用户,而不是为了方便其创建者。

次要目的是消除名称冲突的歧义。实际上,名称冲突非常罕见。 (如果命名空间的主要目的是消除冲突,那么可以想象基类库中的命名空间会少得多!)

使用命名空间时还需要考虑其他事项吗?

是的。正确使用命名空间有很多方面。例如:

  • 违反标准命名约定可能会导致混乱。特别是,不要将类命名与其命名空间相同! (有关详细信息,请参阅下面的链接。)
  • 使用命名空间可以使扩展方法发挥您意想不到的作用; 要小心
  • 在存在名称冲突的世界中, “using”指令的精确位置可能会巧妙地改变解析规则;这些情况很少见,但当它们发生时会令人困惑
  • 冲突通常发生在机器生成的代码与人类生成的代码交互的上下文中;在这种情况下要小心,特别是如果您是编写代码生成器的人。保持高度防御性;你不知道编写人类生成的一半的人会造成多么疯狂的名称冲突。

有关更多详细信息,请参阅我关于此主题的文章:

http://blogs.msdn。 com/b/ericlippert/archive/tags/namespaces/

另请参阅框架设计指南,了解有关命名空间使用的正确和不正确约定的更多想法。

它们对性能或类似的东西有影响吗?

几乎从来没有。命名空间是 C# 语言的虚构;底层类型系统没有“命名空间”。当你说

using System;
...
class MyException : Exception 
...

没有名为“Exception”的类时。类名称是“System.Exception”——名称中包含一个句点。 CLR、反射和 C# 语言共同让您相信该类被命名为“Exception”并且位于命名空间“System”中,但一旦您深入幕后,实际上并不存在命名空间这样的野兽。这只是一个惯例,有时您可以省略“系统”。来自名称“System.Exception”。

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

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!)

Are there any other things to consider when using namespaces?

Yes. There are numerous aspects to correct usage of namespaces. For example:

  • violating standard naming conventions can cause confusion. In particular, do not name a class the same as its namespace! (See link below for details.)
  • using a namespace can bring extension methods into play that you didn't expect; be careful
  • where precisely the "using" directive goes can subtly change resolution rules in a world where there are name collisions; these situations are rare, but confusing when they arise
  • collisions often arise in contexts where machine-generated code is interacting with human-generated code; be careful in such situations, particularly if you are the one writing the code generator. Be very defensive; you don't know what crazy name collisions the person writing the human-generated half is going to create.

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.

Do they have an impact on performance or something like that?

Almost never. Namespaces are a fiction of the C# language; the underlying type system does not have "namespaces". When you say

using System;
...
class MyException : Exception 
...

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".

苍景流年 2024-11-13 01:15:57

根据 MSDN 命名空间具有以下属性:

  • 它们组织大型代码项目。
  • 它们用 分隔。操作员。
  • using 指令意味着您不需要为每个类指定命名空间的名称。
  • 全局命名空间是“根”命名空间:global::System 将始终引用 .NET Framework 命名空间 System

其次,命名空间与性能无关,但如果您创建了自己的命名空间,那么您应该遵循整个项目的约定。

According to MSDN a namespace has the following properties:

  • They organize large code projects.
  • They are delimited with the . operator.
  • The using directive means you do not need to specify the name of the namespace for every class.
  • The global namespace is the »root« namespace: global::System will always refer to the .NET Framework namespace System.

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.

无语# 2024-11-13 01:15:57

它不会影响性能。但为了代码的可读性,我建议删除不需要的 using 语句

It doesn't affect performance. But for code readability, I would recommended remove unwanted using statements

吃不饱 2024-11-13 01:15:57

命名空间是从早期技术(如 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

蓝色星空 2024-11-13 01:15:57

您已经谈到了两个主要原因。这是来自 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.

中性美 2024-11-13 01:15:57

这些都是那里的大人物。

并没有真正的性能优势。至少,不是直接的。如果没有命名空间框架,则必须搜索更多位置才能找到您尝试包含的代码 - 这几乎就像需要为每个项目加载整个 .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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文