7

Does Visual Studio 2013 have facility for showing unused functions?

What is the best solution to removing those functions?

Mike G
  • 4,232
  • 9
  • 40
  • 66
Arian
  • 12,793
  • 66
  • 176
  • 300

3 Answers3

7

ReSharper can detect and highlight dead code when solution-wide analysis is enabled.

It will report some false positives, so a manual review is still needed. For instance, R# won't detect when a function is used only through reflection, and will consider it's not used. The same goes for things like IoC containers based on conventions etc.

JetBrains provides some custom attributes to decorate your code with (like [UsedImplicitly]). They guide the R# analysis engine and document your code.

Lucas Trzesniewski
  • 50,214
  • 11
  • 107
  • 158
  • It is commercial solution, not all developers can use it. – Admiral Land Dec 10 '15 at 12:06
  • 2
    @user1405423 sure, but it **is** still a solution and the answer is valid, I don't feel your downvote is justified. Besides, if you're a professional C# developer (which a lot of us here are), you probably should at least try this plugin. – Lucas Trzesniewski Dec 10 '15 at 12:24
7

If you enable Code Analysis (Project => Properties => Code Analysis), you will get a list of issues. Unused methods or variables are in this list as well as a lot of other potential issues.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • 2
    It might be worth mentioning that the `Code Analysis` is not available in any Express Version of Visual Studio - at least until version 2013 – mfeineis Aug 05 '14 at 11:15
  • 1
    Hmm. This doesn't appear to be working for me (VS 2013 Pro). Find All References only returns the method definition but it doesn't show up in Code Analysis. – Mike G Aug 28 '15 at 15:06
2

You can use the tool NDepend for hunting unused functions. NDepend is integrated in Visual Studio let's write code rule as C# LINQ queries. Around 200 default code rules are provided and 3 of them are about:

Such query can be executed and edited live in Visual Studio and matched methods (here unused methods) are listed.

Potentially dead method in visual studio with ndepend

Patrick from NDepend team
  • 13,237
  • 6
  • 61
  • 92