tags:

views:

174

answers:

7

What is the quickest way to determine which members of an enum are not being used?

A: 

Ctrl-F and search the entire namespace/project for that member of enum

+3  A: 

Using find references on each member of the enum is the fastest way I can think of.

jfclavette
I was hoping there was something faster than doing them one at a time. Might not be.
JC
A: 

Commenting / uncommenting members. If the compiler does not throw an error the enum member is not used.

Update: As mentioned in the comments this of course only works for projects contained in the solution / the active build configuration. The same holds for the Find References and Ctrl+F methods.

Otherwise there is also the option to do a file search, e.g. using grep. However, this option only allows to do a string-based search and does not resolve any types.

0xA3
...assuming of course all of the referencing projects are in the solution.
Codebrain
+5  A: 

Comment the enum members out one by one and see if your code compiles. If compilation breaks, the member is used. Although this method only catches compile-time use of your enums. They could be used at runtime.

LeonZandman
+1 for mentioning run-time uses.
Lucas Jones
This would take quite a while to do with a decent size enum and code base. I was hoping for a resharper/other tool solution that could do this in one pass.
JC
A: 

If you are using VS2005/8 Ctrl-Shift-F so search in the files. This will give you a list of files that you can double-click to goto the lines.

If you don't use VS then you can use WinGrep which will do the same thing (without the double-click feature)

caveman_dick
+2  A: 

If you're using ReSharper, click on the enum to check, hit Alt+F7 (Shift+F12 if you're using VS shortcuts), and it'll give you a list of every place it's used in your entire solution.

Chris Doggett
+1  A: 

Being on the safe side you can mark your members with ObsoleteAttribute. Adding [Obsolete(true)] will fail the build if given member is used.

This can obviously be used not only for enums but for nearly anything in .NET.

Resharper is your tool of choice if you need to delete the members from a solution and you're not worried about another uses in different solutions.

Rashack