How to remove unused using namespaces

后端 未结 9 1767
刺人心
刺人心 2020-12-07 22:14

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.

相关标签:
9条回答
  • 2020-12-07 22:51

    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 PowerCommands to remove unused usings for entire projects

    0 讨论(0)
  • 2020-12-07 22:55

    You can install an extension called: Productivity Power Tools 2015 (from Microsoft) and it has an option to automatically remove unused usings when you save your file. No more work needed after.

    0 讨论(0)
  • 2020-12-07 23:00

    If you are using Visual Studio 2019, you can:

    Right click your solution -> Analyze and Code Cleanup -> Configure Code Cleanup -> Create a profile (Remove & Sort usings) and then run it.

    0 讨论(0)
  • 2020-12-07 23:04

    Same answer as everyone else. If you are a keyboard ninja (Shift+F10 pops up contextual menu), use this:

    Shift+F10,O,R


    What do you call that special key for 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:

    whateverThatKeyCalled, O, R

    0 讨论(0)
  • 2020-12-07 23:08

    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.

    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!

    0 讨论(0)
  • 2020-12-07 23:12

    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.

    0 讨论(0)
提交回复
热议问题