在 C# 中使用命名空间 [英] Use of Namespaces in C#

查看:28
本文介绍了在 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:

  • 将项目组织成有意义的部分
  • 区分同名的类

我的问题是:使用命名空间时还有其他需要考虑的事情吗?它们对性能有影响吗?

My Question is: Are there any other things to consider when using namespaces? Do they have an impact on performance or something like that?

推荐答案

据我所知,它们用于两件事:

As far as I know, they are used for two things:

• 将项目组织成有意义的部分

• To structure the project into meaningful pieces

• 区分同名的类

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

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:

  • 违反标准命名约定可能会导致混淆.特别是,不要将类命名为与其命名空间相同的名称!(有关详细信息,请参阅下面的链接.)
  • 使用命名空间可能会带来意想不到的扩展方法;小心
  • 使用"指令的确切位置可以巧妙地改变存在名称冲突的世界中的解析规则;这些情况很少见,但一旦出现就会令人困惑
  • 在机器生成的代码与人工生成的代码交互的情况下,经常会出现冲突;在这种情况下要小心,特别是如果您是编写代码生成器的人.非常防守;你不知道编写人工生成的一半的人会创造什么疯狂的名字冲突.

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

See my articles on this subject for more details:

http://blogs.msdn.com/b/ericlippert/archive/标签/命名空间/

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

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?

几乎没有.命名空间是 C# 语言的虚构;底层类型系统没有命名空间".当你说

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

没有名为Exception"的类.类名是System.Exception"——名称中有句点.CLR、反射和 C# 语言都让你相信这个类被命名为异常"并且它位于命名空间系统"中,但是一旦你进入幕后,真的没有命名空间这样的野兽.这只是一个约定,有时您可以省略系统".来自名称System.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".

这篇关于在 C# 中使用命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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