C# 在嵌套命名空间中使用命名空间指令
是的,我通常使用“using”指令,如下所示,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AwesomeLib
{
//awesome award winning class declarations making use of Linq
}
我最近看到了诸如
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AwesomeLib
{
//awesome award winning class declarations making use of Linq
namespace DataLibrary
{
using System.Data;
//Data access layers and whatnot
}
}
授予的示例,我知道我可以将 USING 放在我的命名空间声明中。如果您的名称空间位于同一根(它们组织起来)中,那么这样的事情对我来说是有意义的。
System;
namespace 1 {}
namespace 2
{
System.data;
}
但是嵌套命名空间又如何呢?就我个人而言,我会将所有 USING 声明保留在顶部,以便您可以轻松找到它们。相反,看起来它们分布在整个源文件中。
在嵌套命名空间中以这种方式使用 USING 指令有好处吗?比如内存管理或者JIT编译器?
Right, I've usually used 'using' directives as follows
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AwesomeLib
{
//awesome award winning class declarations making use of Linq
}
i've recently seen examples of such as
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AwesomeLib
{
//awesome award winning class declarations making use of Linq
namespace DataLibrary
{
using System.Data;
//Data access layers and whatnot
}
}
Granted, i understand that i can put USING inside of my namespace declaration. Such a thing makes sense to me if your namespaces are in the same root (they organized).
System;
namespace 1 {}
namespace 2
{
System.data;
}
But what of nested namespaces? Personally, I would leave all USING declarations at the top where you can find them easily. Instead, it looks like they're being spread all over the source file.
Is there benefit to the USING directives being used this way in nested namespaces? Such as memory management or the JIT compiler?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
IL 代码不反映 C#
使用
运行时性能
因为您询问的是运行时性能,所以我们来看看源代码下面发生了什么。
如果您使用 Microsoft 的 IL 反汇编工具查看编译后的 IL 代码< /a> (正如我们在这里所做的那样)无论程序员如何在源代码中使用
using
,您都会看到所有类名始终都是完全限定的。在下面的已编译 IL 代码示例中,请注意,尽管
using
位于原始 C# 源代码文件中,但没有看到“快捷方式”机制。例如,IL 描述了一个长扩展 [System.Web]System.Web.UI.Page
,而 C# 则使用: Page
以及使用 System.Web。 UI;
(两个单独的语句)。在编译的 IL 中,无论如何,所有类都是完全限定的。
这意味着根据设计时
using
语句,运行时不会有任何性能优势或缺点。编译时间
根据您在源代码中分散
using
和命名空间
的方式,可能会有或多或少的关键字挂起大约。编译器必须查看并处理所有这些内容,但总体而言,与编译器为制作成品而必须执行的所有操作相比,对于这种微不足道的事情,编译性能可以忽略不计。设计时优势
命名空间是一种组织技术,
using
是一种在源代码级别管理它们的方法(并指示编译器如何使用它们,以便它可以相应地编译程序)。当 C# 源指定using System.Web.UI;
时,不会导入任何内容,并且文件大小不会变大(因为程序集已被引用);相反using
只是在使用using
的范围内对该命名空间的内容实现更短的语法,无论是在整个文件范围或文件内声明的命名空间范围。如果明智地使用它们,程序员的好处是可以减少多个命名空间
using
之间的模糊类名冲突。源代码命名空间的组织在已编译的 IL 代码中以不同的方式表示(如上面的示例所示)。
IL Code does Not reflect C#
using
Runtime Performance
Because you're asking about runtime performance, here's a look into what's happening underneath the source code.
If you look at the compiled IL code with Microsoft's IL Diassembler tool (as we're doing here) you will see all class names are fully qualified all the time no matter how the programmer used
using
in the source code.In the following sample of compiled IL code notice no "shortcut" mechanism is seen although
using
was in the original C# source code files. For example IL describes one longextends [System.Web]System.Web.UI.Page
whereas C# would have used: Page
and alsousing System.Web.UI;
(two separate statements).In the compiled IL all classes are fully qualified regardless.
This means there are no performance benefits or drawbacks at runtime based on the design time
using
statements.Compile Time
Depending on how you scatter about your
using
s andnamespace
s in the source code, there might be more or less of the keywords hanging around. The compiler has to see them all and process them all but overall the compile performance would be negligible for something this trivial, compared to all things a compiler has to do to make the finished product.Design Time Benefits
namespaces are an organizational technique and
using
is a way of managing them at the source code level (and to instruct the compiler how you're using them so it can compile the program accordingly). When C# source specifiesusing System.Web.UI;
, nothing is imported and the file size doesn't grow larger (because the assembly is already referenced in); insteadusing
is simply effecting a shorter syntax to the contents of that namespace, from within the scope theusing
is used in, whether that be in the entire file scope or a declared namespace scope inside the file.The benefit to the programmer is reduction of ambiguous class name conflicts between multiple namespace
using
s if they are judiciously used.Organization of source code namespaces is represented differently in the compiled IL code (as seen in the above example).
当您处于不需要
System.Data
的作用域时,如果 IntelliSense 不触发System.Data
成员会更好,因为它会干扰您的其他事情正在寻找关于一些性能问题 - 我认为你喜欢如何使用它并不重要......
When you are in a scope that doesn't need
System.Data
it is better if the IntelliSense doesn't trigger theSystem.Data
members just to mess with other things you are looking forAbout some performance issue - I don't think it matters how you prefer to use it...
生成的中间语言没有任何好处。由于 CIL 是 JIT 编译器的输入,因此也不受影响。
既然您谈论的是最佳实践:最佳实践是每个文件只有一个类,因此您也只有一个命名空间。
There's no benefit to the generated intermediate language. Since the CIL is the input for the JIT compiler, this is also unaffected.
Since you're speaking about best practises: it's best practise is to have only one class per file, so you'd have only one namespace as well.
来自风格警察规则:
From the Style Cop rules:
using 子句仅有助于使您的代码更易于维护,据我所知,它们不会影响代码的性能。
这同样适用于命名空间 - 您可以将所有类放在全局命名空间中,并将它们命名为 Class1、Class2 等,并且您的代码也将执行同样的操作。但这将是一场噩梦!
所以它们都是为了帮助编码员,它们不会影响编译后的代码。
The using clauses only help making your code more maintainable, they don't affect the performance of your code AFAIK.
The same applies to namespaces - you could put all your classes in the global namespace and name them Class1, Class2 and so on, and your code would perform just as well. But it would be a nightmare to maintain!
So they are both there to help you, the coder, they don't affect the compiled code.