Is the Find All References in Visual Studio good enough to judge if a piece of code is unused?

Viewed 127

I'm refactoring a big C# project and it's only natural to find my snowman's unused dead code.

There's this function called FooBar() which looks useful since it has a bunch of code inside it. As is obvious, I was trying to find the references to this function using the "Find All References (F9)" option and it showed up empty-handed (even with the scope to Entire Solution).

Here is the function:

Class A has:

~
public virtual void FooBar(){

<function is empty>

}
~

I have a Class B with:

public override void FooBar(){

~<doing something important here>~

}

I've ran the "Find All References" on both these functions and nothing showed up. I'm assuming its safe to remove but worried if it is, indeed, used somewhere. Apart from this, I've used the "Find in Files" (with Solution-wide scope) for the method name and it showed up empty too.

My question is: Does "Find All References" obtain all the usages? Can it be relied upon to find and remove dead code if nothing shows up in its results?

EDIT:

This function is not part of an API and would not be called by any external code.

2 Answers

No, there are various reason this will not find code that relies on it.

  1. It is a public API that is consumed by other projects.
  2. The code is called by reflection.
  3. The code is in files that are not analysed, for example Razor files are often skipped.

I'm sure there are more reasons beside these.

It is useful, but don't rely completely on it.

Even if the "Find All References" does not show any usages, it is still possible that the method is used somewhere. Maybe via reflection, or dynamic objects, or if the code is inside a library, some outer application that is using this library can be using this method without Visual Studio knowing.

My advice is to research more thoroughly than just "Find All References".

Related