Quick Tip - Remove Unused Using References in Visual Studio

marteaga

Mark Arteaga

Posted on August 1, 2021

Quick Tip - Remove Unused Using References in Visual Studio

When working on a C# project be it Xamarin, ASP.NET Core etc within Visual Studio, developers sometimes forget to clean up using statements. Now Visual Studio does a good job of showing you visually unused using statements as follows

Screenshot 2021-08-01 150011

When you are working on bigger projects (in my current case, working on a project with 774 files), sometimes as a developer we forget to clean up our using statements as we go through code refactoring.

Well, thanks to Visual Studio, we can quickly clean up using statements in a file in two ways.

First is putting your cursor on one of the grayed out lines and clicking the light bulb, then clicking "Remove Unnecessary Usings". (NOTE: The shortcut key for this is CTRL-.)

Screenshot 2021-08-01 150730

When the action is executed the references will be cleaned up. But what happens when you have 700+ files? 

Well if you noticed in the dialog, there is a section at the bottom that says

Screenshot 2021-08-01 151508

Here you can see the options are

  1. Document
  2. Project
  3. Solution

Now it's pretty obvious, what the options are, but in all my years of using Visual Studio, I have not noticed these options and no one wants to go through 700+ files to clean them up!

This is what the dialog looks like when you click Solution

Screenshot 2021-08-01 164316

So I don't know if this is a Visual Studio 2019 feature or if it's always been there. But this makes it a little more 'enjoyable' to go in and clean up lines of code that are no longer required in your source code!

💖 💪 🙅 🚩
marteaga
Mark Arteaga

Posted on August 1, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related