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.
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!
CTRL + K + E Will sort and remove your unused usings.
Shift + Alt + O will take care of you.
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
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
In my case, it is binded as ⌘+R+G
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With