保持“清洁”的环境有什么好处? C# 中的 using 指令列表?

发布于 2024-07-07 11:56:14 字数 136 浏览 6 评论 0原文

我知道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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

╭ゆ眷念 2024-07-14 11:56:14

如果您始终只有所需的 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.

抠脚大汉 2024-07-14 11:56:14

对我来说,这基本上都是为了减少噪音(加上让 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.

凉宸 2024-07-14 11:56:14

没有运行时影响。 这纯粹是编译时间。 它可能会影响以下方面:

  1. 命名空间冲突的机会更少
  2. 代码文件中的“噪音”更少 非常
  3. 明确文件中预期的命名空间和可能的类型
  4. 使用菜单删除未使用的和排序意味着开发人员之间使用语句更加一致。 减少为了修复问题而进行愚蠢检查的可能性。

There's no runtime impact. It's purely compile time. It potentially impacts the following:

  1. Less chance for Namespace collisions
  2. Less "noise" in the code file
  3. Very explicit about which namespaces and possible types to expect in the file
  4. Using the menu to remove unused and Sort means more consistency with using statements among the devs. Less chance of dumb checkins just to fix it up.
独自唱情﹋歌 2024-07-14 11:56:14
  1. 噪音较小。
  2. 对使用什么类型有明确的期望(“我的 UI 层依赖于 System.Net。哇,为什么?”)
  3. 更干净的引用:如果您有最少的 using 语句集,则可以清理您的引用。 我经常看到开发人员只是不断地将引用放入他们的项目中,但当不再需要它们时,他们从不删除它们。 如果您没有任何实际需要引用的内容(并且 using 语句很重要),那么清理引用就变得微不足道。 (为什么要这样做?在已分解为组件的大型系统中,它将通过消除未使用的依赖来简化构建依赖项。)
  1. Less noise.
  2. Clear expectation of what types are used ("My UI layer depends upon System.Net. Wow, why?")
  3. Cleaner references: if you have the minimal set of using statements, you can cleanup your references. Often I see developers just keep throwing references into their projects, but they never remove them when they're no longer needed. If you don't have anything that actually needs a reference (and a using statement counts), it becomes trivial to clean up your references. (Why would you want to do that? In large systems that have been decomposed into components it will streamline your build dependencies by eliminating the unused deps.)
歌入人心 2024-07-14 11:56:14

对我来说,一开始的清晰的 using 语句列表可以很好地理解预期的类型。

For me, a clean list of using statements at the beginning can give a good understanding of the types to expect.

夕嗳→ 2024-07-14 11:56:14

几年前,当我第一次安装 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.

初与友歌 2024-07-14 11:56:14

我无法谈论编译时间和性能方面的好处,但是如果您最大限度地减少了 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.

Hello爱情风 2024-07-14 11:56:14

编译时存在一个差异:当您删除引用,但代码中仍然有 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文