limscoder
limscoder

Reputation: 3167

Finding unused classes in C# app

I'm a C#/.net/Visual Studio noob. I inherited a half-completed C# application for a mobile phone. In the course of debugging, I came across several half-finished classes that don't seem to be used anywhere else in the code. Is there a way to get determine if a class definition is instantiated anywhere?

Upvotes: 8

Views: 2662

Answers (7)

Jeff Meatball Yang
Jeff Meatball Yang

Reputation: 39017

Remove them from the project and let your unit tests (ahem, you have those right?) and your QA team (you have that right?) identify the problems.

Jokes aside, if it's SO obvious that it's not complete, why not simply remove the code and recompile?

The next steps I would take would be to use a tool like "Find All References" or Resharper (does it even have a feature to do that?)

Upvotes: 0

Vlad
Vlad

Reputation: 35584

You can list all the classes (searching for class [a-zA-Z0-9_]+), and then search for new <classname>. The ones not found at the second search are not used. Of course, a simple script in your favourite script language would help.

You'll need however to filter out the classes that are used as base classes of used classes.

Note that this way you'll not find the classes which are used only from unused classes, so several iterations might be needed. Moreover, if some two classes are using each other (but not used from outside), removing them might need additional effort.

Edit:
A better approach would be building dependency tree: for each of the classes you define which class is used by that class, and which class is a base class for that class. This way you find which classes are required for every single class. Then, you can define which classes are required (directly or indirectly) from the class containing Main. All other classes are "unreachable" and therefore not used.

This approach will however remove the classes instantiated by reflection. Well, there is no way to find out at compile time, which classes are going to be instantiated by reflection anyway.

Maybe using the ready tools (like others proposed) is a simpler alternative.

Upvotes: -1

Greg Bogumil
Greg Bogumil

Reputation: 1923

Unless you know the code, and the modules that may use it., CodeRush or Resharper are your better choices.

None of the other answers mentioned the modifiers which can be applied to classes/functions. You certainly want to take scope into consideration before deleting code. You may have other assemblies which use classes/functions.

Upvotes: 0

Eric King
Eric King

Reputation: 11734

I usually start with Shift-F12 (or right-click on class name and select "Find All References")

Upvotes: 1

Andrew Hare
Andrew Hare

Reputation: 351506

The quickest way (in Visual Studio) is to right-click the type name and select Find all references from the context menu. This will show you all places where that type is referenced in the current solution.

Upvotes: 15

Adam Lear
Adam Lear

Reputation: 38768

Without ReSharper or a similar tool, you can always do a file search for "new ClassName(" in the entire solution.

Upvotes: 2

Dead account
Dead account

Reputation: 19960

You should get Resharper - it will show "dead" code in grey and make refactoring a lot easier! You may also prefer CodeRush.

Upvotes: 3

Related Questions