how to detect unused imports?

我怕爱的太早我们不能终老 提交于 2020-06-27 06:48:04

问题


In my code im often adding some import statements like e.g.:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;

During development some of these import statements may become unnecessary because i move the code that required these imports. In eclipse, unused imports are marked by the IDE, but in Visual Studio 2010 I couldn't find any hint. How can i detected them?


回答1:


Visual Studio has this feature built in if you want to remove them rather than just detect them: Automatic Code Generation -> Organize usings

The Remove Unused Usings option in the Visual Studio user interface removes using directives, using aliases, and extern aliases that are not used in the source code. There are two ways to call the operation:

  • Main Menu - On the Edit menu, point to IntelliSense, point to Organize Usings, and then click Remove Unused Usings.

  • Context Menu - Right-click anywhere inside the code editor, point to Organize Usings, and then click Remove Unused Usings.

Edit to include information from comments

Franci has chimed in with the Powershell extension to VS 2010 which adds in more functionality to do this task as well as others: http://visualstudiogallery.msdn.microsoft.com/en-us/e5f41ad9-4edc-4912-bca3-91147db95b99




回答2:


Visual Studio 2019 comes with default code cleanup that fixes this. Shortcut is to press CTRL+K and CTRL+E as default.

https://docs.microsoft.com/en-us/visualstudio/ide/code-styles-and-code-cleanup?view=vs-2019



来源:https://stackoverflow.com/questions/4147035/how-to-detect-unused-imports

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!