C#使用命名空间语句排序
我想我在某处读到,排序你的 using 语句并删除未使用的语句会带来某种性能优势......但我似乎找不到任何证据或资源来支持这一点......这有什么道理吗?
I thought I read somewhere that ordering your using statements and getting rid of unused ones had some kind of performance benefit...but I can't seem to find any evidence or resources to support that...is there any truth to this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不会,用于设置命名空间的
using
语句没有性能成本。无论语句的顺序如何,生成的 IL 代码都将相同。唯一的优点是清晰度和可读性。此外,删除未使用的会加快编译时间,但运行时性能不会有任何不同。No, the
using
statement, for setting the namespaces, has no performance cost. The IL code produced will be the same, regardless of the order of the statements. The only advantage would be for clarity and readability. Also, removing unused ones would speed up compilation time, but run time performance won't be any different.来自
msdn
,您非常想要
有用的链接
来参观。From the
msdn
,Very much
useful link
you would like to visit.其他评论者未提及的另一个问题:您的 using 语句会影响 Intellisense 中显示的内容(特别是扩展方法)。删除未使用的语句将使您的 Intellisense 性能更好,并且仅显示相关的扩展方法。
An additional issue not mentioned by other commenters: Your using statements affect what appears in Intellisense (particular for extension methods). Removing unused statements will keep your Intellisense performing better and only showing relevant extension methods.
不会有的。至少在运行时肯定不是。在构建时可能需要一些额外的时间来处理它们,但这并不明显。
using
语句不会转移到 IL 代码中,所有内容都有其全名,因此它只是构建时的事情。现在,就我个人而言,我热键“排序并删除使用”并不断点击它,只是因为它使代码保持干净。但这只是一种强迫症:)
There would not be. At least certainly not at runtime. It is possible that at build time it takes some extra time to process them, but it wouldn't be noticable. The
using
statements aren't carried over into the IL code, everything has its full name instead, so it is only a build-time thing.Now, personally, I hot-key "sort and remove usings" and hit it constantly, just because it keeps the code cleaner. But it is just an obsessive-compulsive thing :)