Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove unused using namespaces

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.

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.

like image 986
user1496355 Avatar asked Aug 22 '12 14:08

user1496355


People also ask

How do I delete unused namespaces 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 namespaces in Visual Studio 2019?

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

How do I delete all unused imports in Visual Studio?

Shift + Alt + O will take care of you.


2 Answers

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

like image 176
saj Avatar answered Oct 13 '22 01:10

saj


In Visual Studio 2017, I use this very simple key combinations: CTRL+R+G to remove unused imports/namespaces

Update:

In VS for Mac:

Unfortunately, there's no default key binding for this but you can always edit your preferences.

Visual Studio -> Preferences

enter image description here In my case, it is binded as +R+G

like image 34
mr5 Avatar answered Oct 13 '22 02:10

mr5