I have to refactor a large C# application, and I found a lot of functions that are never used. How can I check for unused code, so I can remove all the unused functions?
-
4possible duplicate of [What tools and techniques do you use to find dead code in .NET?](http://stackoverflow.com/questions/162641/what-tools-and-techniques-do-you-use-to-find-dead-code-in-net) – Alex Angas Jul 08 '10 at 23:47
-
2possible duplicate of [Is there a tool for finding unreferenced functions (dead, obsolete code) in a C# app?](http://stackoverflow.com/questions/65585/is-there-a-tool-for-finding-unreferenced-functions-dead-obsolete-code-in-a-c) – nawfal Oct 12 '13 at 17:51
-
6I'm surprised that this is labelled as off topic, I found the question and answers useful 11 years after the question was written. the off-topic link provided says that "... software tools commonly used by programmers; and is ..." is definitely relevant for SO!. – shelbypereira Jan 16 '20 at 13:19
-
1Still usefull 12 years later. – Sylvain Rodrigue Sep 10 '21 at 19:50
-
Still useful 14 years later. – Sylvain Rodrigue Jul 27 '23 at 18:01
9 Answers
Yes, ReSharper does this. Right click on your solution and selection "Find Code Issues". One of the results is "Unused Symbols". This will show you classes, methods, etc., that aren't used.

- 13,237
- 6
- 61
- 92

- 19,333
- 6
- 58
- 52
-
20this is great. not enough people know about this. You have to turn on Solution Wide Analysis also to get everything to show up. – mcintyre321 Sep 23 '10 at 12:50
-
18Resharper is a great tool, but I found it to be unreliable for this task. I have a public method where I've removed all references. If I right-click the method and select Show Usages, there are none, but Resharper's code issues doesn't list it as unused. – user890155 Aug 11 '11 at 14:55
-
On Jarrett Meyer's answer, right-click on "project" instead of "solution" to save search time in a multi-project solution. – fab Aug 16 '11 at 19:15
-
10We're using dependency injection. As a result, everything looks used to resharper because even unused types are still being registered with unity. – Montgomery 'monty' Jones Oct 14 '11 at 16:02
-
-
-
15@user890155 That would be because the method is public, the library could be consumed by another application not in the current solution. I believe it will only flag internal and private methods as being code issues if unused. – Lukazoid Nov 26 '12 at 16:21
-
3@elggarc Regarding dependency injection, take a look at Agent Mulder plugin mentioned here: http://blogs.jetbrains.com/dotnet/2012/08/resharper-70-plug-ins/ Project homepage: http://hmemcpy.github.com/AgentMulder/ Agent Mulder — support for Dependency Injection frameworks such as Autofac, Castle Windsor, Unity. Since ReSharper doesn’t know about these containers, classes can frequently be marked as unused, or not instantiated. Agent Mulder tells ReSharper when these classes are being used, and provides navigation to the registration point from each class. – Grzegorz Smulko Jan 02 '13 at 13:27
-
Agent Mulder looks like it will do the trick when running inspections locally. If you're running inspections on a build server, though, you may be out of luck. AFAIK, the ReSharper command line runner used in TeamCity doesn't support plugins. http://blog.jetbrains.com/dotnet/2013/01/24/agent-mulder-supporting-ioc-in-resharper/#comment-71446 – kiprainey Jan 22 '14 at 17:12
-
1This process with screenshots (including how to turn on solution-wide analysis): https://chrisseroka.wordpress.com/2013/10/28/find-unused-private-and-public-methods-with-resharper/ – Jeff B May 28 '14 at 16:32
-
ReSharper can be a good solution of course. I just have no idea why they have made it so expensive. People have made entire SDKs for much much cheaper prices. – disasterkid Aug 24 '15 at 07:39
-
There are a dozen duplicates of this exact same question that all reference R#, should they all be flagged Advertising at this point? – EKW Mar 27 '18 at 18:07
-
1The most recent version of resharper doesn't seem to have this "Unused Symbols" any longer. But within the source code, if the function name is grey (not black) indicates it's not used. – Derek Wade Oct 02 '20 at 03:11
It's a great question, but be warned that you're treading in dangerous waters here. When you're deleting code you will have to make sure you're compiling and testing often.
One great tool come to mind:
NDepend - this tool is just amazing. It takes a little while to grok, and after the first 10 minutes I think most developers just say "Screw it!" and delete the app. Once you get a good feel for NDepend, it gives you amazing insight to how your apps are coupled. Check it out: http://www.ndepend.com/. Most importantly, this tool will allow you to view methods which do not have any direct callers. It will also show you the inverse, a complete call tree for any method in the assembly (or even between assemblies).
Whatever tool you choose, it's not a task to take lightly. Especially if you're dealing with public methods on library type assemblies, as you may never know when an app is referencing them.

- 295,962
- 43
- 465
- 541

- 3,126
- 3
- 23
- 23
-
4Another word of caution, if your app is asp.net, with NDepend you will need to precompile your site so you can analyze the code-behinds and NDepend cannot cover/know about calls from the aspx pages (ie method calls in ObjectDataSources and the like) – Jaime Jul 08 '10 at 23:55
Resharper is good for this like others have stated. Be careful though, these tools don't find you code that is used by reflection, e.g. cannot know if some code is NOT used by reflection.

- 9,970
- 5
- 28
- 34
As pointed Jeff the tool NDepend can help to find unused methods, fields and types.
To elaborate a bit, NDepend proposes to write Code Rule over LINQ Query (CQLinq). Around 200 default code rules are proposed, 3 of them being dedicated to unused/dead code detection
Basically such a rule to detect unused method for example looks like:
// <Name>Dead Methods</Name>
warnif count > 0
from m in Application.Methods where !m.MethodsCallingMe.Any()
select m
But this rule is naive and will return trivial false positives. There are many situations where a method is never called yet it is not unused (entry point, class constructor, finaliser...) this is why the 3 default rules are more elaborated:
- Potentially dead Types (hence detect unused class, struct, interface, delegate...)
- Potentially dead Methods
- Potentially dead Fields
NDepend integrates in Visual Studio 2022, 2019, 2017,2015, 2013, 2012, 2010, thus these rules can be checked/browsed/edited right inside the IDE. The tool can also be integrated into your CI process and it can build reports that will show rules violated and culprit code elements. NDepend has also a VS Team Services extension.
If you click these 3 links above toward the source code of these rules, you'll see that the ones concerning types and methods are a bit complex. This is because they detect not only unused types and methods, but also types and methods used only by unused dead types and methods (recursive).
This is static analysis, hence the prefix Potentially in the rule names. If a code element is used only through reflection, these rules might consider it as unused which is not the case.
In addition to using these 3 rules, I'd advise measuring code coverage by tests and striving for having full coverage. Often, you'll see that code that cannot be covered by tests, is actually unused/dead code that can be safely discarded. This is especially useful in complex algorithms where it is not clear if a branch of code is reachable or not.
Disclaimer: I work for NDepend.

- 13,237
- 6
- 61
- 92
-
Hi, how can I delete them quickly using NDepend? I found that this only found them out. – Just a learner Apr 22 '21 at 02:04
I would also mention that using IOC aka Unity may make these assessments misleading. I may have erred but several very important classes that are instantiated via Unity appear to have no instantiation as far as ReSharper can tell. If I followed the ReSharper recommendations I would get hosed!

- 61
- 1
- 1
ReSharper does a great job of finding unused code.
In the VS IDE, you can right click on the definition and choose 'Find All References', although this only works at the solution level.

- 295,962
- 43
- 465
- 541
The truth is that the tool can never give you a 100% certain answer, but coverage tool can give you a pretty good run for the money.
If you count with comprehensive unit test suite, than you can use test coverage tool to see exactly what lines of code were not executed during the test run. You will still need to analyze the code manually: either eliminate what you consider dead code or write test to improve test coverage.
One such tool is NCover, with open source precursor on Sourceforge. Another alternative is PartCover.
Check out this answer on stackoverflow.
I have come across AXTools CODESMART..Try that once. Use code analyzer in reviews section.It will list dead local and global functions along with other issues.

- 1,019
- 3
- 15
- 41
FXCop is a code analyzer... It does much more than find unused code. I used FXCop for a while, and was so lost in its recommendations that I uninstalled it.
I think NDepend looks like a more likely candidate.