views:

5127

answers:

6

How can I find and delete unused references in my projects?

I know you can easily remove the using statements in vs 2008, but this doesn't remove the actual reference in your projects. The referenced dll will still be copied in your bin/setup package.

+3  A: 

Resharper will do this for you (and so so much more!)

Chris Canal
Will Resharper actually remove the reference to the DLL in the References folder? I don't think it does this?
Tobin Harris
I don't think it will... I think it just yanks the using statement for a namespace you don't reference.FWIW - Coderush offers the same functionality "and so much more!" :)
Soulhuntre
A: 

Ah, it will? Has it always been able to do this?

Earlier on I loved resharper, untill it broke the double-tab functionality. Then I realised I actually like my visual studio without all the resharper magics.

Maybe I should give it another shot at some point..

borisCallens
It's greys them out and when you alt-enter on them it will give you the option to clear them out. You can also set it up with the auto Code Clean util.
Chris Canal
Does it still break the double tab function?
borisCallens
double tab function?
Svish
@Svish: Are you going to be in a world of joy :D Please open up any csharp code file and type "if" (without quotes) and then press the tab key twice :) I think this function is what I like most about VS ;)
borisCallens
@Chris: I think you are refering to the "using" statements. This functionality also comes out of the box. It's the references (in your project file) I want to prune.
borisCallens
For future readers: I am giving resharper another shot and seems it has become much more lightweight :)I sent a request to my boss for buying it. Also, with the new VS2010 plugin architecture coming up I'm expecting lots of goodies from the resharper camp :)
borisCallens
@boris: Nicetip, didn't know it before!
SyaZ
Yeah baby, yeah!
borisCallens
A: 

Given that VisualStudio (or is it msbuild?) detects unused references and doesn't include them in the output file, you can write a script which parses the references out of the csproj, and compares that with the referenced Assemblies detected by reflexion on the project output.

If you're motivated...

Benjol
This is not always correct. For example, if you have class Foo which derives from Bar, and you have static member Blah in Bar, and you call it as Foo.Blah, then you have to have assembly of Foo in references for compiler, but it will not be emitted into output, because static member is in Bar.There are some more cases like this.
Ilya Ryzhenkov
Yeah, I had noticed some 'holes' in my script, never got round to working out why. Thanks for the info.
Benjol
+19  A: 

*Note: see http://www.jetbrains.net/devnet/message/5244658 for another version of this answer.

Reading through the posts, it looks like there is some confusion as to the original question. Let me take a stab at it.

The original post is really asking the question: "How do I identify and remove references from one Visual Studio project to other projects/assemblies that are not in use?" The poster wants the assemblies to no longer appear as part of the build output.

In this case, ReSharper can help you identify them, but you have to remove them yourself.

To do this, open up the References inth Solution Browser, right mouse click on each referenced assembly, and pick "Find Dependent Code". See:

http://www.jetbrains.com/resharper/features/navigation%5Fsearch.html#Find%5FReferencedDependent%5FCode

You will either get:

  1. A list of the dependencies on that Reference in a browser window, or

  2. A dialog telling you "Code dependent on module XXXXXXX was not found.".

If you get the the second result, you can then right mouse click the Reference, select Remove, and remove it from your project.

While you have to to this "manually", i.e. one reference at a time, it will get the job done. If anyone has automated this in some manner I am interested in hearing how it was done.

You can pretty much ignore the ones in the .Net Framework as they don't normally get copied to your build output (typically - although not necessarily true for Silverlight apps).

Some posts seem to be answering the question: "How do I remove using clauses (C#) from a source code file that are not needed to resolve any references within that file".

In this case, ReSharper does help in a couple ways:

  1. Identifies unused using clauses for you during on the fly error detection. They appear as Code Inspection Warnings - the code will appear greyed out (be default) in the file and ReSharper will provide a Hint to remove it:

    http://www.jetbrains.com/resharper/features/code%5Fanalysis.html#On-the-fly%5FError%5FDetection

  2. Allows you to automatically remove them as part of the Code Cleanup Process:

    http://www.jetbrains.com/resharper/features/code%5Fformatting.html#Optimizing%5FNamespace%5FImport%5FDirectives

Finally, realize that ReSharper does static code analysis on your solution. So, if you have a dynamic reference to the assembly - say through reflection or an assembly that is dynamically loaded at runtime and accessed through an interface - it won't pick it up. There is no substitute for understanding your code base and the project dependencies as you work on your project. I do find the ReSharper features very useful.

jlo
Nice extensive answer. What's weird is that although you know I'm not referring to the using statements (that clean-up is standard in VS, don't get why R# re-invented that) you answer that question first. Would you mind switching the two around?
borisCallens
Will do. ReSharper's feature precedes the Visual Studio implementation. It remains as it is achieved through their "autocorrect" feature - the Smart Tags are turned off when you have ReSharper.
jlo
+3  A: 

Removing unused references is a feature Visual Studio 2008 already supports. Unfortunately, only for VB .NET projects.

I have opened a suggestion on Microsoft Connect to get this feature for C# projects too:

http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=510326

If you like this feature as well then you might vote my suggestion.

jbe
A: 

PowerCommands for Visual Studio 2008 (free from MSDN Code Gallery) has a "Remove and Sort Usings" command in a context menu that can be executed at the file, folder, project, and solution levels. It has many other nice features as well, including an "Undo Close" pane for re-opening recently closed files.

Travis Heseman