未使用的 using 语句

发布于 2024-10-02 06:12:57 字数 163 浏览 5 评论 0原文

我可能已经知道这个问题的答案,但我认为无论如何还是值得问的。如果我的代码文件中有大量 using 语句,但没有被使用;

  1. 这会对性能产生任何不利影响吗?
  2. 编译器在编译/运行时如何处理它们?

谢谢

I may already know the answer to this question, but I thought it was worth asking anyway. If I have a load of using statements within my code file that aren't being used;

  1. Does that have any sort of detrimental performance impact?
  2. How does the compiler deal with them at compile/run time?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

似最初 2024-10-09 06:12:57

这会对性能产生任何不利影响吗?

不。

编译器在编译/运行时如何处理它们?

在编译时,它们会执行您所期望的操作,即充当命名空间导入。它们不会出现在编译的二进制文件中:对类型的任何引用都使用完全限定名称。

即使不需要,编译器也会检查 using 语句中的命名空间;如果删除命名空间,则引用该命名空间的所有 using 语句都将失败。

对我来说,多余的 using 语句就像多余的注释。它们对编译器没有任何影响,但包含太多,您就有可能让开发人员感到困惑。

does that have any sort of detrimental performance impact?

No.

How does the compiler deal with them at compile/run time?

At compile time they do what you'd expect, i.e. act as namespace imports. They don't appear in the compiled binary: any reference to a type uses the fully-qualified name.

The compiler does check the namespace in a using statement even if it's not needed; if you remove a namespace, then all the using statements that refer to it will fail.

To me, redundant using statements are like redundant comments. They make no difference to the compiler, but include too many of them, and you risk confusing your developers.

多情出卖 2024-10-09 06:12:57

它根本不会影响运行时的性能。

它可能会稍微增加编译时间,因为:

1)编译器需要解析更多字符
2)它必须在更多候选者中查找标识符。但由于这可能使用哈希表,所以它也不应该昂贵。

我猜编译器的速度减慢可以忽略不计

我猜它会减慢 Intellisense 的速度,因为它必须显示和过滤的列表变得相当长。

删除未使用的用法更多的是一种风格而不是性能改进。您需要小心扩展方法,因为它们是通过 using 语句引入范围的。例如,即使当前未使用,我也不会删除 using System.Linq

It doesn't affect performance at runtime at all.

It will probably increase compile-time very slightly since:

1) The compiler needs to parse a few more characters
2) It has to look up identifiers among more candidates. But since this probably uses hashtables it shouldn't be expensive either.

I'd guess the compiler slowdown is negligible.

I'd guess it slows Intellisense down a bit more since the list it has to display and filter gets quite a bit longer.

Removing unused usings is more a stylistic than a performance improvement. And you need to be careful about extension methods since they are brought into scope by using statements. For example I don't remove using System.Linq even when it's currently unused.

爱的那么颓废 2024-10-09 06:12:57

运行时不会影响性能。但是,大量命名空间可能会在一定程度上减慢代码完成速度并降低工作效率

No performance impact in run time. However, a lot of namespaces can somewhat slow down your code completion and decrease productivity

陈年往事 2024-10-09 06:12:57

运行时不会影响性能,使用会在编译时解决,无用的会被忽略。

There is no performance hit at runtime, usings are resolved at compile time and useless ones are ignored.

情感失落者 2024-10-09 06:12:57

它根本没有影响 - 请参阅对此问题的答复:

如何未使用的 using 指令是否会影响性能?

It has no impact at all - see response to this question:

How is performance affected by an unused using directive?

淡淡绿茶香 2024-10-09 06:12:57

它可能会使编译时间更长,但对运行时性能没有影响或影响可以忽略不计。

It may make the compile time longer, but it will have either no or a negligible effect on run-time performance.

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