C# Frequently Asked Questions

The C# team posts answers to common questions

Why doesn't C# warn about unused methods?

Q: Why doesn't C# warn on unused methods?

A: This is something that the C# compiler could do, subject to a few caveats:

  • Virtual functions would have to be excluded
  • Interface implementations wouldn have to be excluded
  • You would get false positives if you used reflection
  • You might get false positives if you used delegates (the compiler might be able to be smart here)

Other than that, it would be possible for us to do this, and we may do it in a future version.

 

 

[Author: Eric Gunnerson]

Published Friday, March 19, 2004 7:45 PM by CSharpFAQ

Comments

 

Ilya Ryzhenkov said:

Also there are special methods for designer, which are usually private and not virtual, e.g. ShouldSerializeNNN, ResetNNN where NNN is property name. This links computer with library and design environment too tight...
March 20, 2004 5:43 AM
 

Sean McElroy said:

Thanks for the reply.

Just for a thought, the compiler could look at the access modifier of the method in determining whether a warning is necessary. For instance, unused internal, protected internal, and private methods could generate warnings, but public methods in class library projects would not.
March 20, 2004 9:17 PM
 

Jeffrey van Gogh said:

FxCop can be used to find unused methods & types.

Look under the Performance rules for "Avoid building non-callable code into assemblies" and "Avoid building uninstantiated internal classes into assemblies". It takes in account delegates & well known latebinding scenarios. Ofcourse it still gives false positives for other reflection scenarios.
March 23, 2004 12:59 PM
 

ok said:

July 18, 2004 12:30 AM
 

RebelGeekz said:

December 28, 2004 4:54 AM
Anonymous comments are disabled

© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker