I just tried FxCop. It does detect unused private methods, but not unused public. Is there a custom rule that I can download, plug-in that will detect public methods that aren't called from within the same assembly?
This might be helpful (depending on what you're doing - I assume that the methods are covered by tests if in a library.):
http://www.truewill.net/myblog/index.php/2008/01/27/detecting_unused_methods_in_c
How would it know that the public methods are unused?
By marking a method as public it can be accessed by any application which references your library.
If a method is unused and public FxCop assumes that you have made it public for external things to access.
If unused public methods lead to FxCop warnings writing APIs and the like would be a pain - you'd get loads of FxCop warnings for methods you intend others to use.
If you don't need anything external to access your assembly/exe consider find-replacing public
with internal
. Your application will run the same and FxCop will be able to find the unreferenced internal methods.
If you do need external access find which methods are really needed to be external, and make all the rest internal.
Any methods you make externally visible could have unit tests too.
Corey, my answer of using FxCop had assumed you were interested in removing unused private members, however to solve the problem with other cases you can try using NDepend. Here is some CQL to detect unused public members (adapted from an article listed below):
// <Name>Potentially unused methods</Name>
WARN IF Count > 0 IN SELECT METHODS WHERE
MethodCa == 0 AND // Ca=0 -> No Afferent Coupling -> The method
// is not used in the context of this
// application.
IsPublic AND // Check for unused public methods
!IsEntryPoint AND // Main() method is not used by-design.
!IsExplicitInterfaceImpl AND // The IL code never explicitely calls
// explicit interface methods implementation.
!IsClassConstructor AND // The IL code never explicitely calls class
// constructors.
!IsFinalizer // The IL code never explicitely calls
// finalizers.
Source: Patrick Smacchia's "Code metrics on Coupling, Dead Code, Design flaws and Re-engineering. The article also goes over detecting dead fields and types.
(EDIT: made answer more understandable)