保持“清洁”的环境有什么好处? C# 中的 using 指令列表?
我知道VS2008具有用于清理使用指令的删除和排序功能,Resharper也是如此。 除了您的代码“干净”并消除引用将来可能不存在的名称空间的问题之外,维护“干净”的 using 指令列表还有什么好处?
代码少? 编译时间更快?
I know VS2008 has the remove and sort function for cleaning up using directives, as does Resharper. Apart from your code being "clean" and removing the problem of referencing namespaces which might not exist in the future, what are the benefits of maintaining a "clean" list of using directives?
Less code?
Faster compilation times?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果您始终只有所需的 using 指令,并且始终对它们进行适当排序,那么当您比较两个版本的代码时,您将永远不会看到不相关的更改。
此外,如果您有一组简洁的 using 指令,那么任何查看代码的人都可以通过查看 using 指令来粗略了解将要使用的内容。
If you always only have the using directives that you need, and always have them appropriately sorted, then when you come to diff two versions of the code, you'll never see irrelevant changes.
Furthermore, if you have a neat set of using directives then anyone looking at the code to start with can get a rough idea of what's going to be used just by look at the using directives.
对我来说,这基本上都是为了减少噪音(加上让 Resharper 高兴!)。
我相信编译时间的任何改进都是最小的。
For me it's basically all about less noise (plus making Resharper happy!).
I would believe any improvement in compilation time would be minimal.
没有运行时影响。 这纯粹是编译时间。 它可能会影响以下方面:
There's no runtime impact. It's purely compile time. It potentially impacts the following:
对我来说,一开始的清晰的 using 语句列表可以很好地理解预期的类型。
For me, a clean list of using statements at the beginning can give a good understanding of the types to expect.
几年前,当我第一次安装 ReSharper(在 18 个项目解决方案上)时,我看到了编译时间的显着增长。 从那时起,它就只是保持清洁。
I saw a decent gain in compile time a few years ago when I first installed ReSharper (on an 18 project solution). Since then its just been about keeping it clean.
我无法谈论编译时间和性能方面的好处,但是如果您最大限度地减少了 using 声明,则命名空间冲突的可能性就会降低。 如果您使用多个第三方库,这一点尤其重要。
I can't speak to the benefits in compile time and performance, but there's a lower chance of namespace collisions if you have minimize your using declarations. This is especially important if you are using more than one third party library.
编译时存在一个差异:当您删除引用,但代码中仍然有 using 指令时,您会收到编译器错误。 因此,拥有一个干净的 using 指令列表可以更轻松地删除未使用的引用。
通常编译器会删除未使用的引用,但我不知道当代码中存在 using 时这是否有效。
There is one compile-time difference: when you remove a reference, but still have a using directive in your code, then you get a compiler error. So having a clean list of using directives makes it a little bit easier to remove unused references.
Usually the compiler removes unused references, but I don't know if that works when a using is in the code.