4

How do you find all the empty methods of a specific type defined in a project? an example use case would be to find all empty Page_Load methods defined in an Asp.Net application.

ravinsp
  • 4,150
  • 2
  • 32
  • 43
  • 1
    possible duplicate of [Visual Studio 2008 / C# : How to find dead code in a project?](http://stackoverflow.com/questions/2020297/visual-studio-2008-c-sharp-how-to-find-dead-code-in-a-project), [Find unused code](http://stackoverflow.com/q/245963), [Is there a tool for finding unreferenced functions (dead, obsolete code) in a C# app?](http://stackoverflow.com/q/65585), [What tools and techniques do you use to find dead code in .NET?](http://stackoverflow.com/q/162641) – Cody Gray - on strike Jul 28 '13 at 13:35
  • 1
    @CodyGray: I don't think this is a duplicate of any of those; this question deals with empty methods that *are* being used (as event handlers) and thus *aren't* dead code. – icktoofay Jul 28 '13 at 21:35

3 Answers3

7

In the visual studio find-tool, set it to use Regular Expressions. Use this expression to find empty methods.

void\ .*\(*\)(\ |(\r\n))*{(\ |(\r\n))*}

To find empty Page_Load methods:

void\ (Page_Load).*\(*\)(\ |(\r\n))*{(\ |(\r\n))*}

All these approaches would work for "void" methods. For other types, you can change the expression or further generalize the expression to match any kind of return type.

ravinsp
  • 4,150
  • 2
  • 32
  • 43
  • Even if you answered your question yourself please mark it as solved. I searched for this and my first look told me this question wasnt solved in here so I had to test it. Its a benifit for everyone having a green hook showing a working solution :) – C4d Jul 28 '16 at 10:54
1

to improve ravinsp's answer a bit, if you want the 'find all' to actually hilight the entire method (so you can do a search and replace to remove them), use regex

 ^.*void\ .*\(*\)(\ |(\r\n))*{(\ |(\r\n))*}
cmsmith81
  • 31
  • 1
0

You could use FxCop (or possibly StyleCop) to detect empty methods. An FxCop rule for detecting empty methods can be found here (although I haven't tested it, you should be able to modify it to avoid removing the methods).

keyboardP
  • 68,824
  • 13
  • 156
  • 205