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

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

问题描述

我想知道,有什么命名空间在C#和其他编程语言的目的是...

据我所知,他们是用于两件事情:


  • 要建立项目结构成有意义的片

  • 要具有相同名称的区分类别

我的问题是:是否有任何其他的东西使用名称空间时要考虑的?难道他们对性能或类似的东西产生影响?


解决方案

  

据我所知,他们是用于两件事情:


  
  

•要建立项目结构成有意义的片


  
  

•要区分同名类


这基本上它。我会添加到您的第一个点的命名空间结构提供的不仅仅是该项目的大,因为名称空间可以跨越项目和组件。我会添加到您的第二个点命名空间的主要的目的是为结构添加到库中的使之变得更容易找到你需要的东西,避免东西你不需要 。即,命名空间是有作为用于图书馆的用户,而不是它的创作者的便利了方便。

A 二次的目的是为了消除歧义的名称冲突。名称冲突在实践中相当罕见。 (如果命名空间的主要目的是消除歧义的冲突,然后人们想象会有在基类库少了很多名称空间!)


  

还有没有其他的东西使用名称空间时要考虑的?


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


  • 违反标准的命名规则可能会导致混乱。尤其是,不要将一个类命名与它的命名空间! (详情请参阅以下链接。)

  • 使用命名空间可以带来扩展方法发挥作用,你没想到的;要小心

  • 其中precisely的使用指令去能微妙地改变在有名称冲突的世界的决议规则;这些情况是罕见的,但令人困惑,当他们出现

  • 碰撞经常出现在机器生成的code与人类产生code交互环境;在这种情况下小心,特别是如果你是一个写code发生器。要非常的防守;你不知道哪根筋名称冲突写人类产生一半的人会去创造。

看我对这个问题的文章了解更多详情:

<一个href=\"http://blogs.msdn.com/b/ericlippert/archive/tags/namespaces/\">http://blogs.msdn.com/b/ericlippert/archive/tags/namespaces/

和也看到了框架设计指南对正确和不正确约定的更多思考空间使用率。


  

他们有对性能或类似的东西产生影响?


几乎没有。命名空间是C#语言的小说;基础类型系统没有命名空间。当你说

 使用系统;
...
类MyException:异常
...

有没有名为异常类。类名是System.Exception的 - 这个名字中有一个时期。 CLR中,反射,和C#语言的所有阴谋,让你相信这个类被命名为异常,它是在命名空间系统,但真的没有这样的野兽为命名空间一旦你在幕后。这只是一个惯例,有时可以省略系统。从名称System.Exception的。

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?

解决方案

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

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

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