How to identify unused classes in the C # project


I've a C# big solution that contains different projects. It contains also a batch with a Main static method. I have to identify and remove all the classes that can't be used starting from this method. Which is the best way to do that? I'm using Microsoft Visual Studio Professional 2015


There are no tools which can do this completely, because

  • System.Reflection and System.CodeDom exist - Is it possible to dynamically compile and execute C# code fragments?
  • New C# code can be generated at run-time, which uses otherwise-unused classes.
  • No tools can predict what that new C# code is (apart from the humans who wrote the code)
  • Dependency Injection libraries (which use System.Reflection behind the scenes) can call "unused" classes. This happens frequently with MVC Controller classes.
  • Razor Views can use classes. These are not compiled by default. Instead, they will crash at runtime if a class is missing.

Assuming no-one is using System.Reflection, you could do it by hand.

For each class:

  • Select it in Visual Studio, right-click then "Find All References"
  • If none found, comment the class out /* */
  • Rebuild all (including Razor views). If no errors found, then the class is unused.