[英]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: 关于使用指令的最佳实践和效率,我的问题是:
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.