Detecting unused methods in C#

01/27/08

Detecting unused methods in C#

In Extreme Programming Adventures in C#, Ron Jeffries mentions that Microsoft Visual Studio won’t inform him when a public method is unreferenced. This is true, but there’s a workaround.

Even with Warning level 4 and with Treat warnings as errors set to All, VS won’t tell you this. (Side note: Setting Treat warnings as errors in the project properties is one of the first things I do on a new project.) It will tell you about unused variables and fields, though.

FxCop does report “Avoid uncalled private code” for private methods. With public methods on public classes, though, some unknown assembly might call them. Clearly we want to know about uncalled code for internal classes. If we make a class internal, FxCop will report on uncalled public methods.

Great! But - how do we test our classes? We want our unit tests in a separate assembly, and other assemblies don’t have access to internal classes. Or at least, they don’t unless they’re friend assemblies. C# lets you specify [assembly: InternalsVisibleTo("UnitTests")] in AssemblyInfo.cs, where UnitTests is the name of another assembly. Now the unit tests can access the internal classes.

There’s a catch, though. As soon as you add that directive, FxCop recognizes that the classes are externally visible. As such, it no longer detects uncalled code. A workaround is to comment out the assembly directive, build, then run FxCop to check for uncalled code. I’m not aware of a way to automate this.

My test code follows. The public static class was necessary to let FxCop know that the internal class was actually instantiated and the Called method called; it would not be needed in real-world code.

namespace UnusedMethodsTest
{
    public static class Foo
    {
        public static void DoIt()
        {
            Class1 c = new Class1();
            c.Called();
        }
    }

    internal class Class1
    {
        public int Called()
        {
            return _Value;
        }

        public void NeverCalled()
        {
            _Value++;
        }

        private int _Value = 4;
    }
}

In AssemblyInfo.cs:

// [assembly: InternalsVisibleTo("UnitTests")]

See also “Friend Assemblies” in the VS Help (C# Programming Guide).


Your Host: webmaster@truewill.net
Copyright © 2000-2013 by William Sorensen. All rights reserved.