为什么要删除不必要的 C# using 指令? [英] Why should you remove unnecessary C# using directives?
问题描述
例如,我很少需要:
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 语句,旨在通过确保当对象超出范围时,其 IDisposable.Dispose 方法被调用.请参阅C#中using"的用法.
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.Dispose method is called. See Uses of "using" in C#.
推荐答案
当你的程序运行时它不会改变任何东西.所需的一切都是按需加载的.因此,即使您有 using 语句,除非您实际使用该命名空间/程序集中的类型,否则不会加载与 using 语句相关联的程序集.
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.
主要是为了个人喜好清理.
Mainly, it's just to clean up for personal preference.
这篇关于为什么要删除不必要的 C# using 指令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!