Jaa


Remove and sort namespace comes handy in Visual Studio

I was fixing the stylecop errors in my code and was bogged down by the most common (but repetitive) warning on namespaces - "Using directives must be sorted alphabetically by the namespaces". There are some third party tools and add-ins available to fix this warning but then I found 2 options available in the Visual Studio to do the same! I found it very useful and hence sharing the same through this post.

Removing and sorting namespaces can be done very easily in two ways.

Method 1 - Context Menu :

Right click anywhere on the code to view the context menu. Hover on "Organise Usings" and there are 3 options available - "Remove Unused Usings", "Sort Usings" and "Remove and Sort".

Click on the desired command!

Method 2 - Tools :

Click on Tools>Options. Click on Keyboard under Environment. Search for Edit.RemoveandSort. This commend removes the unused usings and also sorts them. Now add a shortcut key for this command by pressing the necessary keys and then click Assign. In the screenshot below I have assigned Ctrl+Shift+Q as the shortcut key.

Here you go.... Press Ctrl+Shift+Q to remove and sort usings.

Similarly, shortcut keys can be assigned for Edit.SortUsings (sorts usings) and Edit.RemoveUsings (removes unused usings). 

Hope this will help you to clean-up the style cop warnings quickly and easily!

Comments

  • Anonymous
    March 03, 2011
    Useful!

  • Anonymous
    July 17, 2013
    Thanks for sharing this!

  • Anonymous
    April 01, 2014
    catch (Exception ex) { Common.writeError(ex); return false; }