<p>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?</p>

[{'answer_id': 71538, 'author': 'Loofer', 'author_id': 5552, 'author_profile': 'https://Stackoverflow.com/users/5552', 'pm_score': 2, 'selected': False, 'text': '<p><a href="http://www.ndepend.com/" rel="nofollow noreferrer">NDepend</a> is your friend for this kind of thing</p>\n'}, {'answer_id': 71587, 'author': 'Ian Nelson', 'author_id': 2084, 'author_profile': 'https://Stackoverflow.com/users/2084', 'pm_score': 1, 'selected': False, 'text': '<p>How would it know that the public methods are unused?</p>\n\n<p>By marking a method as public it can be accessed by any application which references your library.</p>\n'}, {'answer_id': 71730, 'author': 'Keith', 'author_id': 905, 'author_profile': 'https://Stackoverflow.com/users/905', 'pm_score': 3, 'selected': False, 'text': "<p>If a method is unused and public FxCop assumes that you have made it public for external things to access.</p>\n\n<p>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.</p>\n\n<p>If you don't need anything external to access your assembly/exe consider find-replacing <code>public</code> with <code>internal</code>. Your application will run the same and FxCop will be able to find the unreferenced internal methods.</p>\n\n<p>If you do need external access find which methods are really needed to be external, and make all the rest internal.</p>\n\n<p>Any methods you make externally visible could have unit tests too.</p>\n"}, {'answer_id': 71929, 'author': 'user7116', 'author_id': 7116, 'author_profile': 'https://Stackoverflow.com/users/7116', 'pm_score': 4, 'selected': False, 'text': '<p>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 <a href="http://www.ndepend.com/" rel="nofollow noreferrer">NDepend</a>. Here is some CQL to detect unused public members (adapted from an article listed below):</p>\n\n<pre><code>// &lt;Name&gt;Potentially unused methods&lt;/Name&gt;\nWARN IF Count &gt; 0 IN SELECT METHODS WHERE\n MethodCa == 0 AND            // Ca=0 -&gt; No Afferent Coupling -&gt; The method \n                              // is not used in the context of this\n                              // application.\n\n IsPublic AND                 // Check for unused public methods\n\n !IsEntryPoint AND            // Main() method is not used by-design.\n\n !IsExplicitInterfaceImpl AND // The IL code never explicitely calls \n                              // explicit interface methods implementation.\n\n !IsClassConstructor AND      // The IL code never explicitely calls class\n                              // constructors.\n\n !IsFinalizer                 // The IL code never explicitely calls\n                              // finalizers.\n</code></pre>\n\n<p>Source: <a href="http://codebetter.com/blogs/patricksmacchia/archive/2008/02/15/code-metrics-on-coupling-dead-code-design-flaws-and-re-engineering.aspx" rel="nofollow noreferrer">Patrick Smacchia\'s "Code metrics on Coupling, Dead Code, Design flaws and Re-engineering</a>. The article also goes over detecting dead fields and types.</p>\n\n<p><em>(EDIT: made answer more understandable)</em></p>\n\n<hr>\n\n<p>EDIT 11th June 2012: <em>Explain new NDepend facilities concerning unused code. Disclaimer: I am one of the developer of this tool.</em></p>\n\n<p>Since NDepend v4 released in May 2012, the tool proposes to write <a href="http://codebetter.com/blogs/patricksmacchia/archive/2008/02/15/code-metrics-on-coupling-dead-code-design-flaws-and-re-engineering.aspx" rel="nofollow noreferrer">Code Rule over LINQ Query (CQLinq)</a>. Around <a href="http://www.ndepend.com/DefaultRules/webframe.html" rel="nofollow noreferrer">200 default code rules</a> are proposed, 3 of them being dedicated to <em>unused/dead code</em> detection:</p>\n\n<ul>\n<li><a href="http://www.ndepend.com/DefaultRules/webframe.html?Q_Potentially_dead_Types.html" rel="nofollow noreferrer">Potentially dead Types</a> (hence detect unused class, struct, interface, delegate...)</li>\n<li><strong><a href="http://www.ndepend.com/DefaultRules/webframe.html?Q_Potentially_dead_Methods.html" rel="nofollow noreferrer">Potentially dead Methods</a></strong> (hence detect unused method, ctor, property getter/setter...)</li>\n<li><a href="http://www.ndepend.com/DefaultRules/webframe.html?Q_Potentially_dead_Fields.html" rel="nofollow noreferrer">Potentially dead Fields</a></li>\n</ul>\n\n<p>These CQLinq code rules are more powerful than the previous CQL ones. 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 <em>only</em> by unused dead types and methods (recursive).</p>\n\n<p>This is <em>static analysis</em>, hence the prefix <em>Potentially</em> in the rule names. If a code element is used <em>only</em> through reflection, these rules might consider it as unused which is not the case. </p>\n\n<p>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 <em>unused/dead</em> 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.</p>\n'}]
['https://Stackoverflow.com/questions/71518', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/9328/']