ravinsp
ravinsp

Reputation: 4240

Find empty methods in visual studio

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.

Upvotes: 4

Views: 1999

Answers (3)

cmsmith81
cmsmith81

Reputation: 31

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))*}

Upvotes: 1

keyboardP
keyboardP

Reputation: 69362

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).

Upvotes: 0

ravinsp
ravinsp

Reputation: 4240

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.

Upvotes: 8

Related Questions