Visual Studio - Why should I remove unused references?

When it comes to writing code, I like everything to be neat and follow a certain set of coding guidelines. I like to adhere to the principle that I always code as if the next person who ends up maintaining my code is a violent psychopath who knows where I live. While it's not a pretty thought, it does remind me that code maintenance is important and it's very likely that someone else might need to maintain your code in the near future. If you regularly write code using Visual Studio as your IDE, you may have a number of libraries and modules referenced in your application. As your application starts to grow and change, you may find that you start to build up quite a few referenced libraries. I've always wondered to myself, does is matter if I have any unused references in my application? Is there any point in removing unused ones, because it doesn't really affect the application....right? Well, it turns out I was wrong.

By removing any unused references in your application, you are preventing the CLR from loading the unused referenced modules at runtime. Which means that you will reduce the startup time of your application, because it takes time to load each module and avoids having the compiler load metadata that will never be used. You may find that depending on the size of each library, your startup time is noticeably reduced. This isn't to say that your application will be faster once loaded, but it can be pretty handy to know that your startup time might get reduced.

If you work with a team of developers that all develop on a single application, you might find that over time your references start to get bloated and there are quite a few that aren't really used. Eventually, the monitoring of dependencies between classes and assemblies can become a hard task.

Another benefit of removing any unused references is that you will reduce the risk of conflicts with namespaces. For example, if you have both System.Drawing and System.Web.UI.WebControls referenced, you might find that you get conflicts when trying to reference the Image class. If you have using directives in your class that match these references, the compiler can't tell which of the ones to use. If you regularly use autocomplete when developing, removing unused namespaces will reduce the number of autocompletion values in your text editor as you type. You may even increase your typing speed, as there are less values to sort through.

There are a number of ways to remove unused references in your application. VB.NET developers have long had this option built into Visual Studio, but for us using C#, we have to use extensions. If you use ReSharper, you may have seen the remove unused references feature when you right click on your references in your project.

ReSharper remove unused references

It will simply do the magic for you and remove any unused modules in your project. However, if you aren't a fan of ReSharper, there are still other options. There is a great extension available for Visual Studio that is free and will do practically the same thing as the ReSharper extension. Entitled the Reference Assistant for Visual Studio, you can download the extension from the Visual Studio Gallery. The extension performs multi-criteria analysis of a project and decides which assemblies are useful for a project and removes unused ones. In order to use the extension, simply right click on your references in the project.

Visual Studio Extension

And you will be presented with a dialog of references that you can choose to remove.

Remove unused references dialog

It also has a handy feature that will remove any related 'using' directives after removing the unused references. To edit this option, navigate to the Options tab in Visual Studio and then look for the References Assistant section.

Remove unused usings directives

This simple trick can keep your project neat and efficient! Get started using it today.


Ryan - 4/30/2014
Does this work with VS 2013?

John Smith - 11/21/2014
Careful about dynamic references or you might break the application by removing something which is actually needed:

Konstantin - 3/7/2015
I am pretty sure that CLR loads assemblies in a lazy way, i.e. assembly only get loaded when some type from it is instantiated at runtime. This means unused references don't affect application startup time.

Alex Essilfie - 8/20/2015
I still do not know why Visual Studio doesn't come with this built-in for other projects. VB.NET projects have had this for as far as I can remember.

Vladimir - 8/26/2015
There is a FREE Visual Studio extension tool called "Productivity Power Tools" that can be set to solve this issue automatically on saving document. Just set (leave selected) options "Format document on Save" and "Remove and Sort usings on save" under Power Commands subcategory, and your code will be well formatted and without unused references.

Rich - 8/26/2015
@Vladimir, what you shared is useful. However, it's not the same thing. You are talking about removing unneeded "using" statements at the head of a source file. The article is talking about removing unused *references* from a project.

visual smarter - 10/25/2015
Nice article. By the way, Visual Smarter has alot of tools including a nice Multiple-Reference Tweaker. You may like it.

Add your comment

300 Characters left

Please fill this in to confirm that you are human