&QUOT之间的差异;采用" (进口)内部或外部的"空间"? [英] Differences between "using" (import) inside or outside of "namespace"?

查看:145
本文介绍了&QUOT之间的差异;采用" (进口)内部或外部的"空间"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:结果
应该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 namespace

I 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 in Outer namespace, before looking on System namespace. And inside that Math namespace there is not any PI.

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

这篇关于&QUOT之间的差异;采用" (进口)内部或外部的"空间"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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