Removing unused code in Visual Studio
Solution 1
ReSharper is the best choice to clean up your code.
You can use it for free thanks to ReSharper Early Access Program.
Solution 2
There are several tools that you can use to do this:
FxCop will only find unused internal and private code. Of course if you make sure you only publicly expose code that needs to be accessible outside your assembly, then that should be good enbough.
Related videos on Youtube
Comments
-
Matthew Layton almost 2 years
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).
-
Tariqulazam over 11 yearsYou can think of using ReSharper. It provides great supports for code cleanup.
-
Matthew Layton over 11 yearsI tried Resharper. It said it was going to take over 5 hours to perform a code cleanup. I am not sure if it is supposed to take that long. I guess it depends on the size of the project...anyway it crashed at some point during the five hours so I may need to do it again!...but thanks...+1 for effort!
-
Matthew Layton over 11 yearsI tried Resharper overnight (as it said it was going to take over 5 hours) but for some reason I when looked at it in the morning it seemed to have crashed and failed to clean my code, however I will investigate this further. Thanks! +1 for effort.
-
Bill Yang over 10 yearsIn latest version of Resharper(7.1.3), you can analyze referneces directly by going ReSharper -> Find -> Optimize References
-
Dmitry Khryukin over 10 years@BillYang 7.1.3 is not the latest version now. but anyway it's good to know!
-
Sahin over 2 yearsIt's not free and especially for a poor country it costs quite a lot.