例如,我很少需要:

using System.Text;

但它总是默认存在的。如果代码中包含不必要的using指令,我假设应用程序将使用更多内存。但是还有什么我应该注意的吗?

同样,如果同一个using指令只在一个文件中使用,而在大多数/所有文件中使用,会有任何区别吗?


编辑:请注意,这个问题不是关于不相关的概念,称为using语句,旨在帮助管理资源,确保当一个对象超出作用域时,它的IDisposable。方法被调用。参见c#中“using”的用法。


当前回答

如果你想保持你的代码干净,不使用的using语句应该从文件中删除。当你在一个需要理解你的代码的协作团队中工作,认为你所有的代码都必须维护,更少的代码=更少的工作,好处是长期的。

其他回答

不要忘记编译器在构建项目时做了很多工作来优化所有内容。使用that在很多地方都被使用,或者1在编译后不应该做不同的事情。

留下额外的using指令是可以的。除去它们有一点价值,但不是很大。例如,它使我的智能感知完成列表更短,因此更容易导航。

编译的程序集不受无关的using指令的影响。

有时我把它们放在一个#区域,并让它折叠;这样可以更清晰地查看文件。在我看来,这是#region为数不多的好用途之一。

代码整洁是很重要的。

当看到多余的使用时,人们开始觉得代码可能没有得到维护,并处于布劳菲尔德路径上。从本质上讲,当我看到一些没有用过的using语句时,我的大脑后部就会升起一面小黄旗,告诉我“谨慎行事”。而阅读产品代码绝不会让你有这种感觉。

所以清理你的使用。不要马虎。激发人们的信心。让你的代码更漂亮。给其他开发者温暖的感觉。

using语句只是阻止您限定所使用的类型。我个人喜欢把它们清理干净。实际上,这取决于如何使用loc度量

除了编码偏好之外,删除未使用的using(s)/命名空间还有几个原因:

removing the unused using clauses in a project, can make the compilation faster because the compiler has fewer namespaces to look-up types to resolve. (this is especially true for C# 3.0 because of extension methods, where the compiler must search all namespaces for extension methods for possible better matches, generic type inference and lambda expressions involving generic types) can potentially help to avoid name collision in future builds when new types are added to the unused namespaces that have the same name as some types in the used namespaces. will reduce the number of items in the editor auto completion list when coding, posibly leading to faster typing (in C# 3.0 this can also reduce the list of extension methods shown)

删除未使用的名称空间不会做什么:

以任何方式改变编译器的输出。 以任何方式改变已编译程序的执行(更快的加载或更好的性能)。

无论是否删除未使用的using,生成的程序集都是相同的。