Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort and Remove unused usings in C# code

Tags:

Is there a way to Sort and Remove Unused usings in C# just like in Visual Studio 2015 and 2017?

If not, could this be an addition to a future Visual Studio Code version?

like image 752
Miguel Moura Avatar asked Mar 28 '17 14:03

Miguel Moura


People also ask

How do I delete all unnecessary Usings in Visual Studio?

Go to Tools > Options > Text Editor > C# > Code Style > Formatting. It has an experimental Format Document Settings where you can have Visual Studio clean up your code. Two of the options are to "Remove unnecessary usings" and "Sort usings". Check these two and you're good to go!

How do I delete unused imports in Visual Studio 2019?

CTRL + K + E Will sort and remove your unused usings.

How do I delete a namespace in Visual Studio?

From Tools > Option, navigate to Keyboard tab. Search for “Edit. RemoveAndSort” which command will to both remove unused usings and sort the usings, then set shortcut for the same, as I did with “Ctrl + Shift + W” , Click on “Assign” and Ok. you are done.


1 Answers

There is a command that is built into vscode to help out removing unused usings.

The default keybinding is defined as:

{ "key": "ctrl+.", "command": "editor.action.quickFix",                       "when": "editorHasCodeActionsProvider &&                                 editorTextFocus &&                                 !editorReadonly" }, 

As for sorting the usings: I did not find anything built explicitly for using statements, but there is a command to sort lines. Add something similar to the following into your keybindings file:

{ "key": "ctrl+q", "command": "editor.action.sortLinesAscending",                       "when": "editorFocus && !editorReadonly" }, 

Then, select the using statements and press ctrl+q to sort them.

like image 161
alexriedl Avatar answered Sep 29 '22 19:09

alexriedl