命名空间之间的区别{using System;与使用系统;命名空间{} [英] Difference between namespace { using System; } vs. using System; namespace {}

查看:70
本文介绍了命名空间之间的区别{using System;与使用系统;命名空间{}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我实际影响方面的区别:

命名空间MyNamespace {

使用系统;


class MyClass {...}

}


vs.


使用系统;

名称空间MyNamespace {


class MyClass {...}

}


是?有一个框架建议,前者被使用(和一些

ASP.Net模板创建这样的类),但我使用后者。我已经试过了两次b $ b但是却无法确定两者之间的差异,所以我很不知道为什么有一个建议在另一个上做一个。

Can someone tell me the difference in terms of actual implications using:
namespace MyNamespace {
using System;

class MyClass {...}
}

vs.

using System;
namespace MyNamespace {

class MyClass {...}
}

are? There''s a framework recommendation that the former be used (and some
ASP.Net templates create classes like this), but I use the latter. I''ve
tried both but could never determine a difference between the two, so I''m
unsure why there''s a recommendation to do one over the other.

推荐答案

我认为如果你在定义了一个名为
的名称空间后面有另一个名称空间{}前woudl保持使用混淆到第一个

命名空间,而后者将是文件的全局。


你在哪里看到后者是优先的?当你创建一个新类时,没有一个寺庙

在命名空间中有使用;它的价格总是在外面。


安迪

I''d think that if you had another namespace { } after the one you
defined, the former woudl keep the using confied to the first
namespace, while the latter would be global to the file.

Where did you see that the latter is prefered? None of the temples
when you create a new class have the using within the namespace; its
always been the outside.

Andy


我猜,但它可能与

使用声明的范围有关。我自己一直使用后者。


我将关注这个帖子,想出实际的

原因。

- Vaibhav
http://www.nagarro.com

I am guessing, but could it have anything to do with the scope of the
using statement. I use the latter one all the time myself.

I will be following this thread with interest to figure out the actual
reasons.

- Vaibhav
http://www.nagarro.com


Keith,


考虑这个例子:


//以下行导入命名空间B类型(Bar1& Bar2)。

使用B;

命名空间ACD

{

//以下行导入命名空间AB类型(Foo1& Foo2)。

使用B;

}


名称空间AB

{

公共类Foo1 {}

公共类Foo2 {}

}


命名空间B

{

公共类Bar1 {}

公共类Bar2 {}

}

如您所见,更改using语句的位置更改

导入的内容。我相信正式的解释是在C#规范(ECMA版本)中的b / b $ 10.7节。


" <由
namespace-member-declaration

在名称空间声明中,其完全限定名称为N,是每个名称空间声明的

名称空间主体其完全合格的

名称是N

或以N开头,后跟一段时间。


这很可能我读过的最混乱的句子!


Brian


Keith Patrick写道:
Keith,

Consider this example:

// The following line imports namespace B types (Bar1 & Bar2).
using B;
namespace A.C.D
{
// The following line imports namespace A.B types (Foo1 & Foo2).
using B;
}

namespace A.B
{
public class Foo1 { }
public class Foo2 { }
}

namespace B
{
public class Bar1 { }
public class Bar2 { }
}
As you can see changing the location of the using statement changes
what it is imported. I believe the formal explanation is in section
10.7 of the C# specification (ECMA version).

"The scope of a namespace member declared by a
namespace-member-declaration
within a namespace-declaration whose fully qualified name is N, is the
namespace-body of every namespace-declaration whose fully qualified
name is N
or starts with N, followed by a period."

That is quite possibly the most confusing sentence I''ve ever read!

Brian

Keith Patrick wrote:
可以有人使用以下方法告诉我实际影响的区别:
命名空间MyNamespace {
使用系统;

类MyClass {...}
}

vs.

使用System;
命名空间MyNamespace {

class MyClass {...}
}

>是吗?有一个框架建议,前者被使用(和一些
ASP.Net模板创建这样的类),但我使用后者。我已经尝试了两种方法,但却无法确定两者之间的区别,所以我不确定为什么建议一方做另一方。
Can someone tell me the difference in terms of actual implications using:
namespace MyNamespace {
using System;

class MyClass {...}
}

vs.

using System;
namespace MyNamespace {

class MyClass {...}
}

are? There''s a framework recommendation that the former be used (and some
ASP.Net templates create classes like this), but I use the latter. I''ve
tried both but could never determine a difference between the two, so I''m
unsure why there''s a recommendation to do one over the other.






这篇关于命名空间之间的区别{using System;与使用系统;命名空间{}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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