How to find and remove unused references in a C# project
Often your projects is created with default references, some of which you will never use. Sometime you add a reference but then end up not using it. Over time you may have over a dozen references in each of a dozen projects and you know you are not using half of them.
I know two tools that will resolve this. One if free and is a plugin only for this feature. The other is a commercial product and has hundred of features.
Remove Unused References – Free Tool
I prefer the free tool by far as it is a simple and easy to use Visual Studio plugin. So I will test it first.
Click the link and it download and installs in seconds.
Then you will see the feature when you right-click on a project in Visual Studio.
Click it and it will remove any references that are not actually being used.
Ok, now lets create a sample project and see if this will work.
I am created a new Console Application and added the following code to the Program.cs
using System; using System.Data; namespace TooManyReferences { class Program { static void Main(string[] args) { // I need System, System.Data, and System.Xml DataTable table = new DataTable(); table.Columns.Add(new DataColumn("FistName",typeof(String))); table.Columns.Add(new DataColumn("LastName", typeof(String))); DataRow row = table.NewRow(); row["FirstName"] = "Jared"; row["LastName"] = "Barneck"; table.Rows.Add(row); Console.WriteLine(table.Rows[0]); } } }
Now lets right-click on the project and choose Remove Unused References.
Unfortunately, my expectation were not met.
Expectatation
The following reference should remain:
- System
- System.Data
- System.XML
My project should build.
Actual Result
The following references remained:
- System.Data
My project failed to build with the following errors
Error 1 The type 'System.Xml.Serialization.IXmlSerializable' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. C:\Users\212070783\Documents\Visual Studio 2010\Projects\TooManyReferences\TooManyReferences\Program.cs 11 13 TooManyReferences Error 2 The type 'System.ComponentModel.ISupportInitialize' is defined in an assembly that is not referenced. You must add a reference to assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. C:\Users\212070783\Documents\Visual Studio 2010\Projects\TooManyReferences\TooManyReferences\Program.cs 11 13 TooManyReferences Error 3 The type 'System.ComponentModel.ISupportInitializeNotification' is defined in an assembly that is not referenced. You must add a reference to assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. C:\Users\212070783\Documents\Visual Studio 2010\Projects\TooManyReferences\TooManyReferences\Program.cs 11 13 TooManyReferences Error 4 The type 'System.ComponentModel.IListSource' is defined in an assembly that is not referenced. You must add a reference to assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. C:\Users\212070783\Documents\Visual Studio 2010\Projects\TooManyReferences\TooManyReferences\Program.cs 11 13 TooManyReferences Error 5 The type 'System.ComponentModel.MarshalByValueComponent' is defined in an assembly that is not referenced. You must add a reference to assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. C:\Users\212070783\Documents\Visual Studio 2010\Projects\TooManyReferences\TooManyReferences\Program.cs 11 13 TooManyReferences
Note: Remember TooManyReferences is the name of my sample project and that is actually confusing in the log.
Conclusion
This tool removes references it should not remove.
If you use this tool, try using it on one project at a time in a large solution, otherwise you could get overwhelmed with failures.
It is pretty easy to add back references, so it is usable, but if it could be taken one step further, to resolve the references needed by objects my code uses, then it would probably be the best and simplest tool out there for this.
Bug Submitted
Remove Unused References – Commerical Tool
Resharper is the commercial tool that solves this problem. Unfortunately it is not free. However, it works well and I don’t currently have a license, but remember that it did not remove needed references when I last used it. Here is a link for more information.
http://www.jetbrains.com/resharper/webhelp/Refactorings__Remove_Unused_References.html