C# 如何删除未使用的 using 命名空间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12075909/
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
How to remove unused using namespaces
提问by user1496355
I am using visual studio 2010 and was wondering if there is a way to automatically remove all the namespaces that aren't being used at the the top of the page e.g.
我正在使用 Visual Studio 2010,想知道是否有办法自动删除页面顶部未使用的所有命名空间,例如
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
in the above example System.Collections.Generic is not being used is there a way to know that without trial and error.
在上面的例子中 System.Collections.Generic 没有被使用,有没有一种方法可以不用反复试验就知道这一点。
采纳答案by saj
Yes you can right click on the page and from the menu select;
是的,您可以右键单击页面并从菜单中选择;
Organise Usings > Remove Unused Usings
Alternatively if you prefer shortcuts, please use;
或者,如果您更喜欢快捷方式,请使用;
Ctrl + R + G
I am using this all the time, to clean up code, and make code compilation quicker.
我一直在使用它来清理代码,并使代码编译更快。
Or you can use PowerCommandsto remove unused usings for entire projects
或者您可以使用PowerCommands删除整个项目的未使用用途
回答by Avada Kedavra
As saj is saying you can use the Remove Unused Usings, which works great and even greater if you sort the usings at the same time. Resharper 4.5 can help you do this for entire projects which is a functionality that is missing (and is missed) in VS2010.
正如 saj 所说,您可以使用Remove Unused Usings,如果您同时对使用进行排序,效果会更好甚至更好。Resharper 4.5 可以帮助您为整个项目执行此操作,这是 VS2010 中缺失(并且缺失)的功能。
More info provided in the following thread: Remove unused Usings across entire assembly
以下线程中提供了更多信息:删除整个程序集中未使用的使用
Tip:The thread above refers to the blog that proposes a macro to remove unused usings across the entire solution. Organize Usings Across Your Entire Solution. I just tried it out, and it worked like a charm!
提示:上面的主题引用了一个博客,该博客提出了一个宏来删除整个解决方案中未使用的用途。组织整个解决方案的使用。我刚刚试了一下,它就像一个魅力!
回答by Michael Buen
Same answer as everyone else. If you are a keyboard ninja (Shift+F10pops up contextual menu), use this:
和其他人一样的答案。如果您是键盘忍者(Shift+F10弹出上下文菜单),请使用以下命令:
Shift+F10,O,R
Shift+ F10, O,R
What do you call that special keyfor contextual menu? Instead of pressing Shift+F10(my keyboard don't have that special key,that's why I uses Shift+F10), that is shorter:
你怎么称呼上下文菜单的那个特殊键?而不是按Shift+ F10(我的键盘没有那个特殊键,这就是我使用Shift+的原因F10),而是更短:
whateverThatKeyCalled, O, R
whateverThatKeyCalled, O,R
回答by Henk-Martijn
回答by mr5
In Visual Studio 2017, I use this very simple key combinations: CTRL+R+Gto remove unused imports/namespaces
在Visual Studio 2017年,我用这个非常简单的按键组合:CTRL+ R+G删除未使用imports/namespaces
Update:
更新:
In VS for Mac:
在 VS for Mac 中:
Unfortunately, there's no default key binding for this but you can always edit your preferences.
不幸的是,这没有默认的键绑定,但您可以随时编辑您的首选项。
Visual Studio->Preferences
Visual Studio->Preferences
回答by Jordan
I know the OP specified VS 2010, but that was a while ago and this feature is built right into VS 2017. From the Menu, Tools -> Options, then find the check box below.
我知道 OP 指定了 VS 2010,但那是不久前的事,此功能已内置到 VS 2017 中。从菜单中,工具 -> 选项,然后找到下面的复选框。
After that, any time you use CTRL+ K, CTRL+ Dthey will be removed.
之后,任何时候使用CTRL+ K, CTRL+D它们都会被删除。
回答by user1069816
For VS 2019 or ReSharper. If you add an unused using to the top of a file such as using System.IO;a light bulb appears. Clicking on the light bulb gives the option to remove the unused usings from either the file, folder, project or entire solution.
对于 VS 2019 或 ReSharper。如果您在文件顶部添加一个未使用的使用,例如using System.IO;灯泡出现。单击灯泡可以选择从文件、文件夹、项目或整个解决方案中删除未使用的用途。
回答by Amir Popovich
回答by Jaider
I am using Visual Studio 2019 and for some reason my project settings have a Warning level of 3 and this prevent Visual Studio from cleaning the usings. Make sure this value is set to 4.
我正在使用 Visual Studio 2019,出于某种原因,我的项目设置的警告级别为 3,这会阻止 Visual Studio 清理使用。确保此值设置为 4。



