34

I want to know if any tool exists for removing unused references (not unused using directives) within a .NET C# project.

dtb
  • 213,145
  • 36
  • 401
  • 431
Cornel
  • 4,652
  • 15
  • 48
  • 57
  • duplicate of http://stackoverflow.com/questions/81597/remove-unused-references-using – Agent_9191 Nov 24 '09 at 08:39
  • Possible Duplicate http://stackoverflow.com/questions/81597/remove-unused-references-using – Jeeva Subburaj Nov 24 '09 at 08:39
  • 14
    Notifying the duplicate post got duplicated.. funny huh... :) – Jeeva Subburaj Nov 24 '09 at 09:10
  • possible duplicate of [Tools for finding unused references](http://stackoverflow.com/questions/142627/tools-for-finding-unused-references) – Steve Guidi Aug 28 '12 at 02:52
  • 2
    Unfortunately, in `VB.NET` this comes out of the box (built in Visual Studio), but not in `C#` ... :-( – Matt Dec 18 '12 at 16:28
  • Possible duplicate of [Removing all unused references from a project in Visual Studio projects](http://stackoverflow.com/questions/3157066/removing-all-unused-references-from-a-project-in-visual-studio-projects) – IROEGBU Oct 26 '16 at 08:24

6 Answers6

17

I am unaware of such a tool off the top of my head - I do know that Visual Studio does not include one by default. However, referenced assemblies that are not actually used by code in the project are automatically removed by the C# compiler from the list of assemblies referenced by the compiled program. If you use Reflector, you can compare the list of actual references to the list of references in the project, and remove the unnecessary ones that the project includes based on that.

Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
  • how can I use it in VS 2008? I cannot find it. – Cornel Nov 24 '09 at 08:35
  • .NET Reflector is a free (and essential) program from redgate: http://www.red-gate.com/products/reflector/ – Sam Harwell Nov 24 '09 at 08:41
  • Reflector is Redgate Reflector which can be found here: http://www.red-gate.com/products/reflector/ – Murph Nov 24 '09 at 08:46
  • 1
    Reflector is no longer free :( – Shevek Apr 12 '11 at 15:08
  • 8
    Using ILSpy, an open-source .NET assembly browser and decompiler, you can get a list of actual references. Open the assembly in ILSpy and look for the 'References' node. Beware that a reference may contain a reference to another assembly! – mathijsuitmegen Apr 27 '11 at 07:43
9

Resharper will certainly do the trick...

Cédric Rup
  • 15,468
  • 3
  • 39
  • 30
2

JetBrains Resharper adds a menu item Remove Unused References to the References node's context menu in the Solution Explorer.

When you click it, it'll scan you project and list references which can be removed. You can further select which ones you want to remove.

David Ferenczy Rogožan
  • 23,966
  • 9
  • 79
  • 68
  • It also includes an "Optimize references" option which parses your source code as well to remove nuget and other items. Very useful. – rollsch Jan 31 '17 at 05:00
0

Resharper has that option.

Carra
  • 17,808
  • 7
  • 62
  • 75
0

ResolveUR - Resolve Unused Referencese Unused References

https://marketplace.visualstudio.com/items?itemName=battas.ResolveUR-ResolveUnusedReferences

Works with Visual Studio 2013, 2015, 2017 and 2019

Yousha Aleayoub
  • 4,532
  • 4
  • 53
  • 64
0

Check out the Productivity Power-tool Plugin

Removing unused references from project is one of the many helpful features it has. It is free and it has some of the R# features.

Laguna
  • 3,706
  • 5
  • 27
  • 42