名称空间 - 重点是什么? [英] namespaces - what's the point?

查看:103
本文介绍了名称空间 - 重点是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



有人可以告诉我实际上是什么命名空间吗?

我注意到当我在C#中开始一个新项目时,它会放置所有内容在与项目同名的

命名空间中。

我发现它们有点烦人,因为我将类的.cs文件保存到我的''泛型

代码''文件夹,然后把它放在另一个项目中,我必须手动去和

更改命名空间名称。我找到了一点PITA。

我使用了VB.NET,发现它默认没有命名空间,但

它可以有他们是否愿意。我还发现虽然C#默认使用它们,但实际上我没有必要让它们编译。

所以 - 它们没有必要看到我不喜欢他们,为什么我不能

关闭它们?


背后的逻辑是什么它们在两种语言中都是可选的,但在CSS#中默认为
默认情况下在VB.NET中默认关闭?

Hi
Can somebody please tell me what namespaces are actually for?

I notice that when I start a new project in C#, it puts everything in a
namespace of the same name as the project.
I found them a bit annoying as when I save a class''s .cs file to my ''generic
code'' folder, and then put it in another project, I have to manually go and
change the namespace name. This I find a bit of a PITA.
I used VB.NET for a bit and found it didn''t have namespaces by default, but
that it can have them if it wants. I also found that although C# has them by
default, I don''t actually have to have them in order to compile.
So - they''re not necessary, and seeing as I don''t like them, why can''t I
turn them off?

And what''s the logic behind them being optional in both languages, but on by
default in C# yet off by default in VB.NET?

推荐答案

Patty O'Dors< Pa ******** @ discussion.microsoft.com>写道:
Patty O''Dors <Pa********@discussions.microsoft.com> wrote:
有人可以告诉我实际上是什么名称空间吗?


这意味着:

o如果你绝对必须,你可以有两种类型相同的名字

在不同的命名空间中。尽管可能会在某个地方尽量避免这种情况,但是值得尽量避免这种情况。

o你可以很容易地看到一个类使用的命名空间,假设它不是$>
明确使用完整类型名称,通过类顶部

的using指令。

o Intellisense只显示相关的类型名称,而不是

显示*每个*类型。

o创建自己的类型时,它会让读者清楚地了解它的类是什么类型 - 例如,如果某个类型在名称空间中,并且名称为BusinessLogic,则名称为$ Business。然后它将与业务相关

逻辑。


这样说:你是否将所有内容保存在一个目录中

你的电脑?使用命名空间的方式与使用

目录的方式类似。

我注意到当我在C#中启动一个新项目时,它会将所有内容放入
与项目同名的命名空间。


您可以更改项目的根命名空间 - 我通常将它改为

为空字符串,以便我的命名空间结构完全匹配

目录结构。

我发现它们有点烦人,因为我将类的.cs文件保存到我的''通用
代码''文件夹中,然后把它放在另一个项目中,我必须手动去改变命名空间名称。我找到了一点PITA。


如果您正在重用代码,您应该有一个包含所有

代码的项目,而不是在每个项目中重复它。

我使用VB.NET了一下,发现它默认没有命名空间,但是如果它需要它可以拥有它们。我还发现,虽然C#默认使用它们,但我实际上并不需要它们来编译。
所以 - 它们没有必要,并且看到我不喜欢喜欢他们,为什么我不能把它们关掉?


好​​吧,如果你想要

,你可以编辑C#用来创建新课程的模板 - 看看


C:\Program Files \ Microsoft Visual Studio .NET 2003 \ VC#\ VSC#Wizards

\ CSharpAddClassWiz \Templates\1033 \NewCSharpFile.cs


你可以编辑那些和类似的模板来摆脱命名空间

声明如果你真的想要。

什么是'它们背后的逻辑在两种语言中都是可选的,但默认情况下在C#中默认关闭但在VB.NET中默认关闭?
Can somebody please tell me what namespaces are actually for?
It means that:
o If you absolutely have to, you can have two types with the same names
in different namespaces. It''s worth trying to avoid this wherever
possible though.
o You can easily see what namespaces a class uses, assuming it doesn''t
explicitly use full type names, through the using directives at the
top of the class.
o Intellisense will only display the relevant type names, rather than
showing *every* type.
o When creating your own types, it gives the reader a clear idea of the
kind of class it is - for instance, if a type is in a namespace with
a name like "BusinessLogic" then it''s going to be related to business
logic.

Put it this way: do you keep absolutely everything in one directory on
your computer? Use namespaces in a similar way to how you''d use
directories.
I notice that when I start a new project in C#, it puts everything in a
namespace of the same name as the project.
You can change the root namespace of the project - I usually change it
to an empty string so that my namespace structure then exactly matches
the directory structure.
I found them a bit annoying as when I save a class''s .cs file to my ''generic
code'' folder, and then put it in another project, I have to manually go and
change the namespace name. This I find a bit of a PITA.
If you''re reusing code, you should have a project containing all that
code, rather than duplicating it in each project.
I used VB.NET for a bit and found it didn''t have namespaces by default, but
that it can have them if it wants. I also found that although C# has them by
default, I don''t actually have to have them in order to compile.
So - they''re not necessary, and seeing as I don''t like them, why can''t I
turn them off?
Well, you can edit the template that C# uses to create a new class if
you want - look at

C:\Program Files\Microsoft Visual Studio .NET 2003\VC#\VC#Wizards
\CSharpAddClassWiz\Templates\1033\NewCSharpFile.cs

You can edit that and similar templates to get rid of the namespace
declaration if you really want.
And what''s the logic behind them being optional in both languages, but on by
default in C# yet off by default in VB.NET?




嗯,你将永远在C#中使用名称空间。你不需要将类型放在除空

名称之外的命名空间中。一个。


我个人建议总是使用命名空间。


-

Jon Skeet - < sk *** @ pobox.com>
http:// www .pobox.com / ~siget

如果回复小组,请不要给我发邮件



Well, you will always be using namespaces to some extent in C#. You
just don''t have to put types in a namespace other than the "empty
name" one.

Personally I''d recommend always using a namespace though.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


你会把每一个都给我您的系统上的单个文件到一个文件夹?不,你

以有意义的方式创建它们,文件夹包含子文件夹等。


如果你没有,你不仅会有成千上万的文件全部在一个地方

(几乎不可能找到任何东西),你只能使用一次文件名
,因为它们都在同一个文件夹中。


命名空间也是如此。


Patty O''Dors <帕******** @ discussions.microsoft.com>在消息中写道

新闻:8D ********************************** @ microsof t.com ...
Would you put every single file on your system into one folder? No, you
origanize them in meaningful ways, with folders that have subfolders etc.

If you didn''t, not only would you have thousands of files all in one place
(making it virtually impossible to find anything), you would only be able to
use a filename once, since they are all in the same folder.

Same reasoning goes for namespaces.

"Patty O''Dors" <Pa********@discussions.microsoft.com> wrote in message
news:8D**********************************@microsof t.com...

有人可以告诉我实际上是什么名称空间吗?

我注意到当我用C#开始一个新项目时,它将所有内容放在与项目同名的名称空间中。
我发现它们有点烦人,因为当我将类的.cs文件保存到我的
''通用代码时''文件夹,然后把它放在另一个项目中,我必须手动转到
并更改命名空间名称。我找到了一点PITA。
我使用了VB.NET,发现默认情况下它没有名称空间,
但如果它需要它可以拥有它们。我还发现虽然默认情况下C#还有
,但实际上我不需要编译它们。
所以 - 它们没有必要,因为我不知道喜欢他们,为什么我不能把它们关闭呢?

这两种语言背后的逻辑是什么,但默认情况下是
在VB.NET中默认关闭C#?
Hi
Can somebody please tell me what namespaces are actually for?

I notice that when I start a new project in C#, it puts everything in a
namespace of the same name as the project.
I found them a bit annoying as when I save a class''s .cs file to my ''generic code'' folder, and then put it in another project, I have to manually go and change the namespace name. This I find a bit of a PITA.
I used VB.NET for a bit and found it didn''t have namespaces by default, but that it can have them if it wants. I also found that although C# has them by default, I don''t actually have to have them in order to compile.
So - they''re not necessary, and seeing as I don''t like them, why can''t I
turn them off?

And what''s the logic behind them being optional in both languages, but on by default in C# yet off by default in VB.NET?



Patty,

除了其他评论。
Patty,
In addition to the other comments.
我使用了VB.NET,发现它默认没有命名空间,
但是


VB.NET有命名空间并且默认为ON! :-)


VB.NET与&之间的区别C#是VB.NET将命名空间设置为

项目级别设置,而C#包含源文件中的命名空间。

您可以使用Project - Properties更改在

VB.NET中使用的根命名空间,通常与项目相同(就像C#一样!)


如果使用ILDASM或引用汇编(VB.NET& C#)你会看到

所有类都在命名空间中!


希望这有助于

Jay


" Patty O''Dors" <帕******** @ discussions.microsoft.com>在消息中写道

新闻:8D ********************************** @ microsof t.com ...嗨
有人可以告诉我实际上是什么命名空间吗?

我注意到当我在C#中开始一个新项目时,它会把所有内容放在一个
与项目名称相同的名称空间。
我发现它们有点烦人,因为当我将类的.cs文件保存到我的
''通用代码''文件夹中,然后把它放进去在另一个项目中,我必须手动转到
并更改命名空间名称。我找到了一点PITA。
我使用了VB.NET,发现默认情况下它没有名称空间,
但如果它需要它可以拥有它们。我还发现虽然默认情况下C#还有
,但实际上我不需要编译它们。
所以 - 它们没有必要,因为我不知道喜欢他们,为什么我不能把它们关闭呢?

这两种语言背后的逻辑是什么,但默认情况下是
在VB.NET中默认关闭C#?
I used VB.NET for a bit and found it didn''t have namespaces by default, but

VB.NET has namespaces and the are ON by default!!! :-)

The difference between VB.NET & C# is that VB.NET puts the namespace as a
project level setting, while C# includes the namespace in the source file.
You can use Project - Properties to change the Root Namespace used in
VB.NET, which is normally the same as the project (just as C# does!)

If you use ILDASM or reference either assembly (VB.NET & C#) you will see
that all classes are in a namespace!

Hope this helps
Jay

"Patty O''Dors" <Pa********@discussions.microsoft.com> wrote in message
news:8D**********************************@microsof t.com... Hi
Can somebody please tell me what namespaces are actually for?

I notice that when I start a new project in C#, it puts everything in a
namespace of the same name as the project.
I found them a bit annoying as when I save a class''s .cs file to my ''generic code'' folder, and then put it in another project, I have to manually go and change the namespace name. This I find a bit of a PITA.
I used VB.NET for a bit and found it didn''t have namespaces by default, but that it can have them if it wants. I also found that although C# has them by default, I don''t actually have to have them in order to compile.
So - they''re not necessary, and seeing as I don''t like them, why can''t I
turn them off?

And what''s the logic behind them being optional in both languages, but on by default in C# yet off by default in VB.NET?



这篇关于名称空间 - 重点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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