为什么要删除不必要的 C# using 指令?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/136278/
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 should you remove unnecessary C# using directives?
提问by steffenj
For example, I rarely need:
例如,我很少需要:
using System.Text;
but it's always there by default. I assume the application will use more memory if your code contains unnecessary using directives. But is there anything else I should be aware of?
但默认情况下它始终存在。如果您的代码包含不必要的using 指令,我认为应用程序将使用更多内存。但是还有什么我应该注意的吗?
Also, does it make any difference whatsoever if the same using directive is used in only one file vs. most/all files?
此外,如果仅在一个文件中使用相同的 using 指令与大多数/所有文件相比,这有什么区别吗?
Edit: Note that this question is not about the unrelated concept called a using statement, designed to help one manage resources by ensuring that when an object goes out of scope, its IDisposable.Disposemethod is called. See Uses of "using" in C#.
编辑:请注意,此问题与称为using 语句的无关概念无关,旨在通过确保当对象超出范围时调用其IDisposable.Dispose方法来帮助管理资源。请参阅C# 中“使用”的使用。
采纳答案by Darren Kopp
It won't change anything when your program runs. Everything that's needed is loaded on demand. So even if you have that using statement, unless you actually use a type in that namespace / assembly, the assembly that using statement is correlated to won't be loaded.
当你的程序运行时它不会改变任何东西。所需的一切都是按需加载的。因此,即使您有该 using 语句,除非您实际使用该命名空间/程序集中的类型,否则不会加载与 using 语句相关联的程序集。
Mainly, it's just to clean up for personal preference.
主要是为了个人喜好清理。
回答by mattlant
Your application will not use more memory. Its for the compiler to find classes you use in the code files. It really doesnt hurt beyond not being clean.
您的应用程序不会使用更多内存。它供编译器查找您在代码文件中使用的类。除了不干净之外,它真的没有伤害。
回答by Jordan Parmer
The 'using' statement does not affect performance as it is merely a helper in qualifying the names of your identifiers. So instead of having to type, System.IO.Path.Combine(...), you can simply type, Path.Combine(...)if you have using System.IO.
'using' 语句不会影响性能,因为它只是限定标识符名称的辅助工具。因此,不必输入System.IO.Path.Combine(...),如果您使用 System.IO,只需输入Path.Combine(...) 即可。
回答by Patrick Desjardins
Do not forget that the compiler do a lot of work to optimize everything when building your project. Using that is used in a lot of place or 1 shouldn't do a different once compiled.
不要忘记编译器在构建项目时做了大量工作来优化所有内容。使用它在很多地方使用,或者 1 不应该在编译后做不同的事情。
回答by Josh Sklare
It's personal preference mainly. I clean them up myself (Resharper does a good job of telling me when there's unneeded using statements).
主要看个人喜好。我自己清理它们(Resharper 很好地告诉我什么时候不需要 using 语句)。
One could say that it might decrease the time to compile, but with computer and compiler speeds these days it just wouldn't make any perceptible impact.
可以说它可能会减少编译时间,但随着计算机和编译器的速度,现在它不会产生任何可察觉的影响。
回答by Carra
They are just used as a shortcut. For example, you'd have to write: System.Int32 each time if you did not have a using System; on top.
它们只是用作快捷方式。例如,如果您没有 using System,则每次都必须编写: System.Int32; 在上面。
Removing unused ones just makes your code look cleaner.
删除未使用的代码只会让您的代码看起来更干净。
回答by Franci Penov
There's no IL construct that corresponds to using
. Thus, the using
statements do not increase your application memory, as there is no code or data that is generated for it.
没有对应于 的 IL 构造using
。因此,这些using
语句不会增加您的应用程序内存,因为没有为其生成代码或数据。
Using
is used at compile time only for the purposes of resolving short type names to fully qualified type names. Thus, the only negative effect unnecessary using
can have is slowing the compile time a little bit and taking a bit more memory during compilation. I wouldn't be worried about that though.
Using
在编译时仅用于将短类型名称解析为完全限定类型名称。因此,唯一不必要的负面影响using
是稍微减慢编译时间并在编译期间占用更多内存。不过我不会担心的。
Thus, the only real negative effect of having using
statements you don't need is on intellisense, as the list of potential matches for completion while you type increases.
因此,拥有using
不需要的语句的唯一真正负面影响是在智能感知上,因为在您键入时要完成的潜在匹配列表会增加。
回答by Pablo Fernandez
You may have name clashes if you call your classes like the (unused) classes in the namespace. In the case of System.Text, you'll have a problem if you define a class named "Encoder".
如果将类称为命名空间中的(未使用的)类,则可能会发生名称冲突。在 System.Text 的情况下,如果您定义一个名为“Encoder”的类,则会出现问题。
Anyways this is usually a minor problem, and detected by the compiler.
无论如何,这通常是一个小问题,并被编译器检测到。
回答by Jay Bazuzi
Leaving extra using
directives is fine. There is a little value in removing them, but not much. For example, it makes my IntelliSense completion lists shorter, and therefore easier to navigate.
留下额外的using
指令很好。删除它们有一点价值,但价值不大。例如,它使我的 IntelliSense 完成列表更短,因此更易于导航。
The compiled assemblies are not affected by extraneous using
directives.
编译后的程序集不受无关using
指令的影响。
Sometimes I put them inside a #region
, and leave it collapsed; this makes viewing the file a little cleaner. IMO, this is one of the few good uses of #region
.
有时我把它们放在一个 中#region
,然后让它折叠起来;这使得查看文件更清晰。IMO,这是#region
.
回答by CheeZe5
The using statement just keeps you from qualifying the types you use. I personally like to clean them up. Really it depends on how a loc metric is used
using 语句只是让您无法限定您使用的类型。我个人喜欢清理它们。实际上这取决于如何使用 loc 指标