Why doesn't C# warn about unused methods?

Published 19 March 04 07:45 PM

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]

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Ilya Ryzhenkov said on March 20, 2004 5:43 AM:
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...
# Sean McElroy said on March 20, 2004 9:17 PM:
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.
# Jeffrey van Gogh said on March 23, 2004 12:59 PM:
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.
# ok said on July 18, 2004 12:30 AM:
my web:
http://www.sj55.com/pic_sort http://www.zw88.com/paopaotang.htm http://www.zw88.com/sj.htm http://www.zw88.com/sm/
http://www.zw88.com/sms/ http://www.zw88.com/zw.htm http://www.resou.com/8888.htm http://www.sj55.com/pic/pic_1130.htm
# RebelGeekz said on December 28, 2004 4:54 AM:
[http://itpeixun.51.net/][http://aissl.51.net/][http://kukuxz003.freewebpage.org/][http://kukuxz001.51.net/][http://kukuxz003.51.net/][http://kukuxz005.51.net/][http://kukuxz002.51.net/][http://kukuxz004.freewebpage.org/][http://kukuxz007.51.net/][http://kukuxz001.freewebpage.org/][http://kukuxz006.51.net/][http://kukuxz002.freewebpage.org/][http://kukuxz004.51.net/][http://kukuxz008.51.net/][http://kukuxz009.51.net/][http://kukuxz005.freewebpage.org/][http://kukuxz006.freewebpage.org/][http://kukuxz007.freewebpage.org/][http://kukuxz009.freewebpage.org/]

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required

This Blog

Syndication

Page view tracker