"之间的差异;采用" (进口)内部或外部的"空间"? [英] Differences between "using" (import) inside or outside of "namespace"?
问题描述
可能重复:结果
的应该Usings是内部或空间外面
块引用>
我已经看到了使用装配的两种方法。有什么区别和使用一个比其他
$ B $使用System.Data这b命名大会
{
的好处;
类Foo {
:
}使用System.Data这
}
;
命名大会
{
类Foo {
:
}
}
解决方案其实两者之间存在一个尖细的区别。
考虑以下例如:
使用系统// File1.cs
;
命名空间Outer.Inner
{
类Foo
{
静态无效的酒吧()
{
双D = Math.PI;
}
}
}
// File2.cs
命名空间外
{
级数学
{
}
}
在这个编译器会得到一个错误,因为它会找到
数学
类外
命名空间的前的看好系统
命名空间。而里面的是的数学
命名空间没有任何PI
。
在情况下,如果你的,相反,把
使用系统
在类的声明,它将被编译器的第一个的,所以没有错误会产生被拾起。
这样做的好处是显而易见的:在情况下,如果你得到一些代码,其中有人写了
数学
类的的相同的命名空间给你写,你有机会要解决这个问题。
有关更详细的解释一下这个链接(从那里我gracefuly复制的代码太)应该Usings是内部或命名空间一外>
Possible Duplicate:
Should Usings be inside or outside the namespaceI have seen two approaches of using assemblies. What is the difference and benefits of using one over other
namespace Assembly { using System.Data; class Foo{ ... } } using System.Data; namespace Assembly { class Foo{ ... } }
解决方案There is actually a tinny difference between them.
Consider following example:
//File1.cs using System; namespace Outer.Inner { class Foo { static void Bar() { double d = Math.PI; } } } // File2.cs namespace Outer { class Math { } }
Compiler on this will get a error, as it will find
Math
class inOuter
namespace, before looking onSystem
namespace. And inside thatMath
namespace there is not anyPI
.In case, if you, instead, put
using System
inside the class declaration, it will be picked by compiler by first, so no error will be produced.The benefit of this is clear: in case if you get some code, where someone wrote
Math
class in the same namespace that wrote you, you have a chance to workaround that problem.For more detailed explanation look on this link (from where I gracefuly copied the code too) Should Usings be inside or outside the namespace
这篇关于"之间的差异;采用" (进口)内部或外部的"空间"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!