C# 在嵌套命名空间中使用命名空间指令

发布于 2024-08-17 01:43:37 字数 840 浏览 6 评论 0原文

是的,我通常使用“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

無處可尋 2024-08-24 01:43:37

IL 代码不反映 C# 使用

运行时性能

使用有好处吗
以这种方式使用的指令
嵌套命名空间? 比如内存
管理还是 JIT 编译器?

因为您询问的是运行时性能,所以我们来看看源代码下面发生了什么。

如果您使用 Microsoft 的 IL 反汇编工具查看编译后的 IL 代码< /a> (正如我们在这里所做的那样)无论程序员如何在源代码中使用 using,您都会看到所有类名始终都是完全限定的。

下面的已编译 IL 代码示例中,请注意,尽管 using 位于原始 C# 源代码文件中,但没有看到“快捷方式”机制。例如,IL 描述了一个长扩展 [System.Web]System.Web.UI.Page,而 C# 则使用 : Page 以及 使用 System.Web。 UI;(两个单独的语句)。

// ***** 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 中,无论如何,所有类都是完全限定的。

这意味着根据设计时 using 语句,运行时不会有任何性能优势或缺点。

编译时间

根据您在源代码中分散using命名空间的方式,可能会有或多或少的关键字挂起大约。编译器必须查看并处理所有这些内容,但总体而言,与编译器为制作成品而必须执行的所有操作相比,对于这种微不足道的事情,编译性能可以忽略不计。

设计时优势

命名空间是一种组织技术,using是一种在源代码级别管理它们的方法(并指示编译器如何使用它们,以便它可以相应地编译程序)。当 C# 源指定 using System.Web.UI; 时,不会导入任何内容,并且文件大小不会变大(因为程序集已被引用);相反 using 只是在使用 using 的范围内对该命名空间的内容实现更短的语法,无论是在整个文件范围或文件内声明的命名空间范围。

如果明智地使用它们,程序员的好处是可以减少多个命名空间 using 之间的模糊类名冲突。

源代码命名空间的组织在已编译的 IL 代码中以不同的方式表示(如上面的示例所示)。

IL Code does Not reflect C# using

Runtime Performance

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.

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 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)

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 usings and namespaces 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 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 usings if they are judiciously used.

Organization of source code namespaces is represented differently in the compiled IL code (as seen in the above example).

淡看悲欢离合 2024-08-24 01:43:37

当您处于不需要 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 the System.Data members just to mess with other things you are looking for

About some performance issue - I don't think it matters how you prefer to use it...

帝王念 2024-08-24 01:43:37

生成的中间语言没有任何好处。由于 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.

贪了杯 2024-08-24 01:43:37

来自风格警察规则

  1. 将 using-alias 指令放置在命名空间内可以消除冲突类型之间的编译器混淆。
  2. 当在单个文件中定义多个命名空间时,将 using 指令放置在命名空间元素范围内的引用和别名中。

From the Style Cop rules:

  1. Placing using-alias directives within the namespace eliminates compiler confusion between conflicting types.
  2. When multiple namespaces are defined within a single file, placing using directives within the namespace elements scopes references and aliases.
对不⑦ 2024-08-24 01:43:37

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文