Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing unused code in Visual Studio [duplicate]

In relation to this question: "Remove unused references (!= "using")", I would like to know if there is a tool for removing unused classes, structs, delegates, etc from a Visual Studio solution.

Scenario:

I have an unorganised Visual Studio Solution which consists of 1000's of:

  • Native method imports
  • Structures
  • Delegates
  • Enumerations

Rather than trawling through each file clicking "Find All References" and determining if the code is being used somewhere, is there any mechanism by where I can simply remove redundant code files easily?

Example:

//This class contains a method called getRandomValue which returns type RANDOM public class NativeMethods {     [DllImport("random.dll")]     public static extern RANDOM getRandomValue(); }  //This is the RANDOM object as referenced by getRandomValue(); [StructLayout(LayoutKind.Sequential)] public struct RANDOM {     uint a;     uint b;     uint c; }  //This is redundant since nothing is referencing it. [StructLayout(LayoutKind.Sequential)] public struct MESSAGE {     IntPtr sender;     IntPtr recipient;     char[] mText; } 

Note to self:

My gut feeling is that this is going to be tricky since unlike Java, object names do not have to be identical to the file name, and multiple object declarations can reside within a single file, however in this instance (my scenario) every object is declared within its own file (with an identical name).

like image 497
Matthew Layton Avatar asked Oct 08 '12 23:10

Matthew Layton


People also ask

How do I remove unused from Visual Studio 2022?

Navigate to Tools > Options > Text Editor > Code Cleanup. Add a check in the "Run Code Cleanup profile on Save." Be sure to select the appropriate profile you want to execute automatically whenever you save!

Where is the unused code in Visual Studio code?

To find unused members with a Code Analysis Ruleset, from the Visual Studio menu select File -> New -> File… -> General -> Code Analysis Rule Set. Uncheck all the rules. There are many rules we don't care about right now – and some we probably won't ever care about.

How do I optimize imports in VSCode?

In VSCode, go to File -> Preferences -> Settings and click on the icon in the top right hand corner to open up the settings in JSON. Et voilà! Your imports will now be organized every time you save a file.


1 Answers

ReSharper is the best choice to clean up your code.

You can use it for free thanks to ReSharper Early Access Program.

enter image description here

like image 101
Dmitry Khryukin Avatar answered Sep 29 '22 09:09

Dmitry Khryukin