Visual Studio 2008 / C# : How to find dead code in a project?

TalkingCode picture TalkingCode · Jan 7, 2010 · Viewed 12.9k times · Source

How do I find dead code in a Visual Studio 2008 C# project? Like unused classes, unused variables or unused resources?

Answer

João Angelo picture João Angelo · Jan 7, 2010

You can try FxCop, which is integrated in Visual Studio 2008 by the name of Code Analysis. You just have to right click the project file and 'Run Code Analysis'.

FxCop is an application that analyzes managed code assemblies (code that targets the .NET Framework common language runtime) and reports information about the assemblies, such as possible design, localization, performance, and security improvements.

The active rules can be configured in the Code Analysis section of the project properties. For example some rules relevant to the case in hand are present in Usage Rules and Performance Rules:

  • CA1801: Review unused parameters.
  • CA1811: Avoid uncalled private code.

And for greater flexibility you also write your own custom rules (Tutorial on writing your own Code Analysis rule).