使用嵌套的命名空间namespace指令C# [英] C# using namespace directive in nested namespaces
问题描述
右键,我常用使用指令为
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.
有没有造福于使用指令正在使用这种方式嵌套的命名空间?如内存管理和JIT编译器?
Is there benefit to the USING directives being used this way in nested namespaces? Such as memory management or the JIT compiler?
推荐答案
运行时性能
的有没有造福于使用
指令正在使用这种方法在
嵌套的命名空间? 如内存
管理或JIT编译器?的
Is there benefit to the USING directives being used this way in nested namespaces? Such as memory management or the JIT compiler?
由于你 。再询问运行时的性能,下面就来看看到正在发生的事情的源代码的下方。
Because you're asking about runtime performance, here's a look into what's happening underneath the source code.
如果你看一下用的Microsoft's IL Diassembler工具(因为我们在这里所做的),你会看到所有的类名是完全合格的所有时间无论怎样的程序员使用在源代码中使用
。
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.
在编译下面的示例IL代码注意到没有捷径机制被认为虽然使用
是原来的C#源代码文件。例如IL描述了一个长扩展[System.Web程序] System.Web.UI.Page
而C#会使用:页
也使用System.Web.UI程序;
(两个独立的语句)
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 long extends [System.Web]System.Web.UI.Page
whereas C# would have used : Page
and also using System.Web.UI;
(two separate statements).
// ***** Compiled MSIL CODE ****
// Notice all fully qualified classes throughout.
//
.class public auto ansi beforefieldinit WebApplication1.process
extends [System.Web]System.Web.UI.Page
{
.field family class [System.Web]System.Web.UI.HtmlControls.HtmlForm form1
.method family hidebysig instance void
Page_Load(object sender,
class [mscorlib]System.EventArgs e) cil managed
{
// Code size 95 (0x5f)
.maxstack 4
.locals init ([0] string strName,
[1] string strTime)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: call instance class [System.Web]System.Web.HttpRequest [System.Web]System.Web.UI.Page::get_Request()
IL_0007: ldstr "name"
IL_000c: callvirt instance string [System.Web]System.Web.HttpRequest::get_Item(string)
在编译的IL所有的类都是完全合格的,无论
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.
编译时间
根据您如何分散你的使用
和命名
S IN的源代码,可能存在的关键字闲逛的更多或更少。编译器必须看到他们,并处理它们,但整体表现编译就可以忽略不计的东西这个微不足道,比起万物编译器做的,使成品。
Depending on how you scatter about your using
s and namespace
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.
设计时间的好处
命名空间是一个组织的技术和使用
在源代码级管理它们的方式(并指示你如何使用它们的编译器,它可以相应地编译程序)。当C#源指定使用System.Web.UI程序;
,没有导入,文件大小不变得更大(因为组件已经引用的);而不是 使用
只是影响一个较短的语法,该命名空间的内容,从范围内的使用
是用来在,不管是在整个文件范围或文件中声明的命名空间范围。
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 specifies using System.Web.UI;
, nothing is imported and the file size doesn't grow larger (because the assembly is already referenced in); instead using
is simply effecting a shorter syntax to the contents of that namespace, from within the scope the using
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.
组织源代码的名称空间是不同的代表编译IL代码(如见于上面的例子)。
Organization of source code namespaces is represented differently in the compiled IL code (as seen in the above example).
这篇关于使用嵌套的命名空间namespace指令C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!