例如,我很少需要:
using System.Text;
但它总是默认存在的。如果代码中包含不必要的using指令,我假设应用程序将使用更多内存。但是还有什么我应该注意的吗?
同样,如果同一个using指令只在一个文件中使用,而在大多数/所有文件中使用,会有任何区别吗?
编辑:请注意,这个问题不是关于不相关的概念,称为using语句,旨在帮助管理资源,确保当一个对象超出作用域时,它的IDisposable。方法被调用。参见c#中“using”的用法。
例如,我很少需要:
using System.Text;
但它总是默认存在的。如果代码中包含不必要的using指令,我假设应用程序将使用更多内存。但是还有什么我应该注意的吗?
同样,如果同一个using指令只在一个文件中使用,而在大多数/所有文件中使用,会有任何区别吗?
编辑:请注意,这个问题不是关于不相关的概念,称为using语句,旨在帮助管理资源,确保当一个对象超出作用域时,它的IDisposable。方法被调用。参见c#中“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,生成的程序集都是相同的。
其他回答
不要忘记编译器在构建项目时做了很多工作来优化所有内容。使用that在很多地方都被使用,或者1在编译后不应该做不同的事情。
代码整洁是很重要的。
当看到多余的使用时,人们开始觉得代码可能没有得到维护,并处于布劳菲尔德路径上。从本质上讲,当我看到一些没有用过的using语句时,我的大脑后部就会升起一面小黄旗,告诉我“谨慎行事”。而阅读产品代码绝不会让你有这种感觉。
所以清理你的使用。不要马虎。激发人们的信心。让你的代码更漂亮。给其他开发者温暖的感觉。
没有对应于使用的IL构造。因此,using语句不会增加应用程序内存,因为没有为它生成代码或数据。
Using仅在编译时用于将短类型名解析为完全限定类型名的目的。因此,不必要的使用所带来的唯一负面影响就是稍微减慢编译时间,并在编译期间占用更多内存。不过我不担心这个。
因此,使用你不需要的语句的唯一真正的负面影响是在智能感知上,因为当你输入时,潜在的匹配列表会增加。
using语句不会影响性能,因为它只是限定标识符名称的一个助手。因此,如果你使用System.IO,你不必输入System.IO.Path.Combine(…),你可以简单地输入Path.Combine(…)。
除了编码偏好之外,删除未使用的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,生成的程序集都是相同的。