简体   繁体   English

可以“使用 <namespace> ;“指令在.NET中更有效率?

[英]Can “using <namespace>;” directives be made more efficient in .NET?

When I create a new class file in C#, the usual structure is as follows: 当我在C#中创建一个新的类文件时,通常的结构如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UnitTest
{
    class Class1
    {
    }
}

StyleCop doesn't like having using directives outside of the namespace, so ideally, I would refactor my code as such: StyleCop不喜欢在命名空间之外使用指令,所以理想情况下,我会重构我的代码:

namespace UnitTest
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    class Class1
    {
    }
}

From my days as a Java programmer, I was taught that it was better to import only the references that were necessary, rather than the entire package like so: 从我作为Java程序员的日子开始,我被告知最好只导入必要的引用,而不是像这样导入整个包:

import foo.bar.MyObject;

instead of 代替

import foo.bar.*;

I know that this can also be done in C#, with the added feature that you can use aliases for types (sometimes useful when working though native code): 我知道这也可以在C#中完成,增加的功能是你可以为类型使用别名(有时在使用本机代码时很有用):

using StringBuilder = System.Text.StringBuilder;

or when using aliases: 或使用别名时:

using HANDLE = System.IntPtr;
using HDC = System.IntPtr;

So my questions, regarding best practice and efficiency of using directives: 关于使用指令的最佳实践和效率,我的问题是:

  1. Is is more efficient to keep using statements inside a namespace, or is this purely a stylistic preference? 是否更有效地在命名空间中使用语句,或者这纯粹是一种风格偏好?
  2. If is more efficient to only include the necessary items, as opposed to the entire namespace? 如果只包含必要的项目更有效,而不是整个命名空间?
  1. There are some fringe cases where it makes a difference but for the majority of cases it is just a stylistic preference. 一些边缘情况会有所不同,但对于大多数情况来说,它只是一种风格偏好。
  2. The using statements just tell the compiler where it can locate the types. using语句只告诉编译器可以在哪里找到类型。 It has no influence on the runtime. 它对运行时没有影响。
  1. nope; 不; stylistic preference, as long as it doesn't introduce any semantic changes; 风格偏好,只要它不引入任何语义变化; the cases where it changes the meaning are very rare (although there used to be a bug with the LINQ-to-SQL code generator that did care about where you put them - this is now fixed) 它改变含义的情况非常罕见(虽然曾经有一个LINQ-to-SQL代码生成器的错误,它确实关心你放置它们的位置 - 现在已修复)
  2. nope; 不; unused directives are not used, and they don't massively impact the compiler 未使用未使用的指令,它们不会对编译器产生重大影响

Note that having too many directives can cause ambiguities; 请注意,指令太多会导致含糊不清; for example there are a myriad of classes called Timer . 例如,有许多名为Timer的类。 For that reason it is worth keeping things tidy. 因此,值得保持整洁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Span可以提高哪些常见操作的效率 <T> ? - Which common operations can be made more efficient by Span<T>? 使用linq可以使其更具可读性吗? - Can this be made more readable by using linq? 可以使分析器自动缩进预处理器指令吗? - Can an analyzer be made that will auto indent preprocessor directives? 命名空间的所有using指令都应该在命名空间内部吗? - Should all the using directives for namespaces be inside the namespace? 删除不必要的命名空间(使用)指令是否有性能提升? - Is there a performance gain in removing unnecessary namespace (using) directives? “using”指令应该在命名空间内部还是外部? - Should 'using' directives be inside or outside the namespace? 通过不使用LOWER函数来覆盖.net身份验证SQL查询,以提高效率 - Overriding the .net authentication SQL queries to be more efficient by not using the LOWER function 这个LINQ可以更高效吗? - Can this LINQ be more efficient? .NET 6 中的 using 语句/指令在哪里 - Where are the using statements/directives in .NET 6 有没有更好的方法来管理在文件头中使用名称空间指令 - Is there a better way to manage using namespace directives in the head of a file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM