C# 未使用的 using 指令如何影响性能?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1162766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 09:42:16  来源:igfitidea点击:

How is performance affected by an unused using directive?

c#.netvisual-studiousing

提问by KdgDev

Visual Studio will automatically create using statements for you whenever you create a new page or project. Some of these you will never use.

每当您创建新页面或项目时,Visual Studio 都会自动为您创建 using 语句。其中一些你永远不会使用。

Visual Studio has the useful feature to "remove unused usings".

Visual Studio 具有“删除未使用的使用”的有用功能。

I wonder if there is any negative effect on program performance if the using statements which are never accessed, remain mentioned at the top of the file.

我想知道如果从未访问过的 using 语句仍然在文件顶部提及,是否会对程序性能产生任何负面影响。

采纳答案by JaredPar

An unused using has no impact to the runtime performance of your application.

未使用的 using 不会影响应用程序的运行时性能。

It can affect the performance of the IDE and the overall compilation phase. The reason why is that it creates an additional namespace in which name resolution must occur. However these tend to be minor and shouldn't have a noticeable impact on your IDE experience for most scenarios.

它会影响 IDE 的性能和整个编译阶段。原因是它创建了一个额外的命名空间,必须在其中进行名称解析。但是,这些往往是次要的,并且在大多数情况下不会对您的 IDE 体验产生明显影响。

It can also affect the performance of evaluating expressions in the debugger for the same reasons.

出于同样的原因,它还会影响调试器中表达式的计算性能。

回答by ChrisV

No, it's just a compile-time/coding style thing. .NET binaries use fully qualified names under the hood.

不,这只是编译时/编码风格的事情。.NET 二进制文件在后台使用完全限定名称。

回答by tvanfosson

No effect on execution speed, but there may be some slight effect on compilation speed/intellisense as there are more potential namespaces to search for the proper class. I wouldn't worry too much about it, but you can use the Organize Usings menu item to remove and sort the using statements.

对执行速度没有影响,但可能对编译速度/智能感知有一些轻微影响,因为有更多潜在的命名空间来搜索合适的类。我不会太担心,但是您可以使用 Organize Usings 菜单项来删除 using 语句并对其进行排序。

回答by Freddy

No, there are several process involved when compiling a program. When the compiler start looking for references (classes, methods) it will use only the ones used on the code. The using directive only tells the compiler where to look. A lot of unused using statement could maybe have a performance issue but just at compile time. At runtime, all the outside code is properly linked or included as part of the binary.

不,编译程序时涉及多个过程。当编译器开始寻找引用(类、方法)时,它将只使用代码中使用的引用。using 指令只告诉编译器在哪里查找。许多未使用的 using 语句可能有性能问题,但只是在编译时。在运行时,所有外部代码都被正确链接或包含为二进制文件的一部分。

回答by Jeff Leonard

Code that does not execute does not affect the performance of a program.

不执行的代码不会影响程序的性能。

回答by Deepak Tekchandani

The following link A good read on why to remove unused referencesexplains how it be useful to remove unused references from the application.

下面的链接A good read about why to remove unused references解释了从应用程序中删除未使用的引用是如何有用的。

Below are the some excerpts from the link:

以下是链接中的一些摘录:

  1. By removing any unused references in your application, you are preventing the CLRfrom loading the unused referenced modules at runtime. Which means that you will reduce the startup time of your application, because it takes time to load each module and avoids having the compiler load metadata that will never be used. You may find that depending on the size of each library, your startup time is noticeably reduced. This isn't to say that your application will be faster once loaded, but it can be pretty handy to know that your startup time might get reduced.

  2. Another benefit of removing any unused references is that you will reduce the risk of conflicts with namespaces. For example, if you have both System.Drawingand System.Web.UI.WebControlsreferenced, you might find that you get conflicts when trying to reference the Imageclass. If you have using directives in your class that match these references, the compiler can't tell which of the ones to use. If you regularly use autocomplete when developing, removing unused namespaces will reduce the number of autocompletion values in your text editor as you type.

  1. 通过删除应用程序中任何未使用的引用,您可以防止CLR在运行时加载未使用的引用模块。这意味着您将减少应用程序的启动时间,因为加载每个模块需要时间并避免编译器加载永远不会使用的元数据。您可能会发现,根据每个库的大小,您的启动时间会明显缩短。这并不是说您的应用程序加载后会更快,但知道您的启动时间可能会减少可能会非常方便。

  2. 删除任何未使用的引用的另一个好处是可以降低与命名空间冲突的风险。例如,如果同时引用System.DrawingSystem.Web.UI.WebControls,您可能会发现在尝试引用Image类时会发生冲突 。如果您的类中有匹配这些引用的 using 指令,编译器将无法判断要使用哪些指令。如果您在开发时经常使用自动完成功能,删除未使用的命名空间将减少您键入时文本编辑器中自动完成值的数量。