为什么要删除 C# 中未使用的 using 指令?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/629667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why remove unused using directives in C#?
提问by bounav
I'm wondering if there are any reasons (apart from tidying up source code) why developers use the "Remove Unused Usings
" feature in Visual Studio 2008?
我想知道是否有任何原因(除了整理源代码)为什么开发人员使用Usings
Visual Studio 2008 中的“删除未使用”功能?
采纳答案by John Feminella
There are a few reasons you'd want to take them out.
有几个原因你想把它们拿出来。
- It's pointless. They add no value.
- It's confusing. What is being used from that namespace?
- If you don't, then you'll gradually accumulate pointless
using
statements as your code changes over time. - Static analysis is slower.
- Code compilation is slower.
- 没有用。它们没有增加任何价值。
- 这很混乱。该命名空间正在使用什么?
- 如果不这样做,那么
using
随着代码的变化,您将逐渐积累无意义的语句。 - 静态分析较慢。
- 代码编译较慢。
On the other hand, there aren't many reasons to leave them in. I suppose you save yourself the effort of having to delete them. But if you're that lazy, you've got bigger problems!
另一方面,没有太多理由将它们留在里面。我想您可以省去删除它们的工作量。但如果你那么懒惰,你就会遇到更大的问题!
回答by Dead account
Code compiles quicker.
代码编译速度更快。
回答by marc_s
I would say quite the contrary - it's extremely helpful to remove unneeded, unnecessary using statements.
我会说恰恰相反 - 删除不需要的、不必要的 using 语句非常有帮助。
Imagine you have to go back to your code in 3, 6, 9 months - or someone else has to take over your code and maintain it.
想象一下,您必须在 3、6、9 个月后返回您的代码 - 否则其他人必须接管您的代码并对其进行维护。
If you have a huge long laundry list of using statement that aren't really needed, looking at the code could be quite confusing. Why is that using in there, if nothing is used from that namespace??
如果您有大量实际上并不需要的 using 语句清单,那么查看代码可能会令人困惑。如果该命名空间中没有使用任何内容,为什么要在那里使用?
I guess in terms of long-term maintainability in a professional environment, I'd strongly suggest to keep your code as clean as possible - and that includes dumping unnecessary stuff from it. Less clutter equals less confusion and thus higher maintainability.
我想就专业环境中的长期可维护性而言,我强烈建议让您的代码尽可能干净 - 这包括从中转储不必要的东西。更少的混乱意味着更少的混乱,从而更高的可维护性。
Marc
马克
回答by cbp
Less options in the Intellisense popup (particularly if the namespaces contain lots of Extension methods).
Intellisense 弹出窗口中的选项较少(特别是如果命名空间包含大量扩展方法)。
Theoretically Intellisense should be faster too.
理论上 Intellisense 也应该更快。
回答by cbp
This seems to me to be a very sensible question, which is being treated in quite a flippant way by the people responding.
在我看来,这是一个非常明智的问题,回答的人以一种相当轻率的方式对待这个问题。
I'd say that any change to source code needs to be justified. These changes can have hidden costs, and the person posing the question wanted to be made aware of this. They didn't ask to be called "lazy", as one person inimated.
我想说对源代码的任何更改都需要证明是合理的。这些变化可能会产生隐性成本,提出问题的人希望了解这一点。他们没有要求被称为“懒惰”,正如一个人所说的那样。
I have just started using Resharper, and it is starting to give warnings and style hints on the project I am responsible for. Amongst them is the removal of redundant using directive, but also redundant qualifiers, capitalisation and many more. My gut instinct is to tidy the code and resolve all hints, but my business head warns me against unjustified changes.
我刚刚开始使用 Resharper,它开始对我负责的项目发出警告和样式提示。其中包括删除多余的 using 指令,还有多余的限定符、大写等等。我的直觉是整理代码并解决所有提示,但我的业务负责人警告我不要进行不合理的更改。
We use an automated build process, and therefore any change to our SVN repository would generate changes that we couldn't link to projects/bugs/issues, and would trigger automated builds and releases which delivered no functional change to previous versions.
我们使用自动构建过程,因此对我们的 SVN 存储库的任何更改都会生成我们无法链接到项目/错误/问题的更改,并且会触发自动构建和发布,而不会对以前的版本提供功能更改。
If we look at the removal of redundant qualifiers, this could possibly cause confusion to developers as classes our Domain and Data layers are only differentiated by the qualifiers.
如果我们考虑删除多余的限定符,这可能会导致开发人员混淆,因为我们的域和数据层仅通过限定符区分。
If I look at the proper use of capitalisation of anachronyms (i.e. ABCD -> Abcd) then I have to take into account that Resharper doesn't refactor any of the Xml files we use that reference class names.
如果我查看正确使用大写字母(即 ABCD -> Abcd),那么我必须考虑到 Resharper 不会重构我们使用该引用类名的任何 Xml 文件。
So, following these hints is not as straight-forward as it appears, and should be treated with respect.
因此,遵循这些提示并不像看起来那么简单,应该受到尊重。
回答by Jeremy C.
It also helps prevent false circular dependencies, assuming you are also able to remove some dll/project references from your project after removing the unused usings.
它还有助于防止错误的循环依赖,假设您还可以在删除未使用的使用后从项目中删除一些 dll/项目引用。
回答by Tim Duncan
Remove them. Less code to look at and wonder about saves time and confusion. I wish more people would KEEP THINGS SIMPLE, NEAT and TIDY. It's like having dirty shirts and pants in your room. It's ugly and you have to wonder why it's there.
删除它们。需要查看和思考的代码更少,可以节省时间和减少混乱。我希望更多的人将事情保持简单、整洁和整洁。这就像在你的房间里有脏衬衫和裤子。它很丑,你必须想知道它为什么在那里。
回答by hypehuman
In addition to the reasons already given, it prevents unnecessary naming conflicts. Consider this file:
除了已经给出的原因之外,它还可以防止不必要的命名冲突。考虑这个文件:
using System.IO;
using System.Windows.Shapes;
namespace LicenseTester
{
public static class Example
{
private static string temporaryPath = Path.GetTempFileName();
}
}
This code doesn't compile because both the namespaces System.IO and System.Windows.Shapes each contain a class called Path. We could fix it by using the full class path,
这段代码无法编译,因为命名空间 System.IO 和 System.Windows.Shapes 都包含一个名为 Path 的类。我们可以使用完整的类路径来修复它,
private static string temporaryPath = System.IO.Path.GetTempFileName();
or we could simply remove the line using System.Windows.Shapes;
.
或者我们可以简单地删除该行using System.Windows.Shapes;
。
回答by Earth Engine
At least in theory, if you were given a C# .cs file (or any single program source code file), you should be able to look at the code and create an environment that simulates everything it needs. With some compiling/parsing technique you may even create a tool to do it automatically. If this is done by you at least in mind, you can ensure you understand everything that code file says.
至少在理论上,如果您得到一个 C# .cs 文件(或任何单个程序源代码文件),您应该能够查看代码并创建一个模拟它需要的一切的环境。通过一些编译/解析技术,您甚至可以创建一个工具来自动执行此操作。如果您至少记住了这一点,则可以确保您理解代码文件所说的所有内容。
Now consider, if you were given a .cs file with 1000 using
directives which only 10 was actually used. Whenever you look at a symbol that is newly introduced in the code that references the outside world, you will have to go through those 1000 lines to figure out what it is. This is obviously slows down the above procedure. So if you can reduce them to 10, it will help!
现在考虑一下,如果给你一个包含 1000using
条指令的 .cs 文件,而实际上只使用了 10 条。每当您查看代码中新引入的引用外部世界的符号时,您都必须通过这 1000 行来弄清楚它是什么。这显然减慢了上述过程。因此,如果您可以将它们减少到 10 个,那将会有所帮助!
In my opinion, the C# using
directive is very very weak, since you cannot specify single generic symbol without genericity being lost, and you cannot use using
alias directive to use extension methods. This is not the case in other languages like Java, Python and Haskell, in those languages you are able to specify (almost) exactly what you want from the outside world. But event then, I will suggest to use using
alias whenever possible.
在我看来,C#using
指令非常非常弱,因为您不能在不丢失通用性的情况下指定单个通用符号,并且不能使用using
别名指令来使用扩展方法。在其他语言(如 Java、Python 和 Haskell)中,情况并非如此,在这些语言中,您可以(几乎)准确地指定您想要从外部世界获得什么。但是事件,我会建议尽可能使用using
别名。
回答by HimBromBeere
Recently I got another reason why deleting unused imports is quite helpful and important.
最近我得到了另一个原因,说明删除未使用的导入非常有用且重要。
Imagine you have two assemblies, where one references the other (for now let′s call the first one A
and the referenced B
). Now when you have code in A that depends on B everything is fine. However at some stage in your development-process you notice that you actually don′t need that code any more but you leave the using-statement where it was. Now you not only have a meaningless using
-directive but also an assembly-referenceto B
which is not used anywhere but in the obsolete directive. This firstly increases the amount of time needed for compiling A
, as B
has to be loaded also.
想象一下你有两个程序集,其中一个引用另一个(现在让我们调用第一个A
和被引用的B
)。现在,当您在 A 中有依赖于 B 的代码时,一切都很好。但是,在开发过程的某个阶段,您会注意到您实际上不再需要该代码,而是将 using 语句保留在原处。现在,您不仅拥有一个无意义的using
-directive,而且还有一个程序集引用,B
该引用除了在过时的指令中之外没有在任何地方使用。这首先增加了编译所需的时间A
,因为B
还必须加载。
So this is not only an issue on cleaner and easier to read code but also on maintaining assembly-references in production-code where not all of those referenced assemblies even exist.
因此,这不仅是关于更清晰、更易于阅读的代码的问题,而且是在生产代码中维护程序集引用的问题,其中并非所有这些引用的程序集都存在。
Finally in our exapmle we had to ship B and A together, although B is not used anywhere in A but in the using
-section. This will massively affect the runtime-performance of A
when loadingthe assembly.
最后,在我们的示例中,我们必须将 B 和 A 一起发送,尽管 B 不在 A 中的任何地方使用,而是在using
-section 中使用。这将极大地影响加载程序集时的运行时性能。A