例如,我很少需要:

using System.Text;

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

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


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


当前回答

代码整洁是很重要的。

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

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

其他回答

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

代码整洁是很重要的。

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

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

除了编码偏好之外,删除未使用的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,生成的程序集都是相同的。

using语句不会影响性能,因为它只是限定标识符名称的一个助手。因此,如果你使用System.IO,你不必输入System.IO.Path.Combine(…),你可以简单地输入Path.Combine(…)。

这主要是个人喜好。我自己清理它们(ReSharper在告诉我什么时候有不需要的using语句方面做得很好)。

有人可能会说,这可能会减少编译时间,但随着计算机和编译器的速度,这不会产生任何可察觉的影响。