All docs
V20.2
20.2
20.1
19.2
The page you are viewing does not exist in version 19.2. This link will take you to the root page.
19.1
The page you are viewing does not exist in version 19.1. This link will take you to the root page.
18.2
The page you are viewing does not exist in version 18.2. This link will take you to the root page.
18.1
The page you are viewing does not exist in version 18.1. This link will take you to the root page.
17.2
The page you are viewing does not exist in version 17.2. This link will take you to the root page.

How to: Remove Unused Members from Code

  • 3 minutes to read

This example describes ways to remove unused members from code.

Use the Remove Unused Member Refactoring

This refactoring allows you to remove an unused member.

For example, the a4 private variable is never used in the following code snippet:

class Program {
    public static double a0 = 0.938229;
    public static double a1 = -0.726534;
    public static double a2 = 0.647538;
    public static double a3 = 0.382564;
    private static double a4 = 0.345623;

    public static int Main() {
        string input;
        double x, y;
        while (true) {
            Console.Write("x = ");
            input = Console.ReadLine();
            try {
                x = Convert.ToDouble(input);
                y = a0 + a1 * x + a2 * Math.Pow(x, 2) + a3 * Math.Pow(x, 3);
                Console.WriteLine($"a0 + a1 * x + a2 * x^2  + a3 * x^3  = {y}");
            }
            catch (FormatException) {
                break;
            }
        }
        return 0;
    }
}

You can use the CRR0026 - Unused member analyzer to detect this unused member.

To remove the unused member, do the following:

  • Place the caret in the unused member name.

  • Use Ctrl + . or Ctrl + ~ to invoke the Code Actions Menu.

  • Select the Remove Unused Member refactoring from the menu.

    Remove Unused Member

After execution, this refactoring removes the a4 variable from the code.

Remove Unused Member

Highlighting of Unused Members

CodeRush can highlight unused members in the code editor. You can use this to determine where to apply the Remove Unused Member refactoring. To enable highlighting of unused members, check the "Highlight unused code in editor" checkbox on the Editor | C# (Visual Basic) | Code Analysis | General option page.

Highlight

NOTE

The "Highlight unused code in editor" setting is in effect when the CRR0026 - Unused member analyzer is registered in Visual Studio's background analysis. Make sure the "Register in VS" option is enabled for this analyzer in the Code Issues Catalog page. Background analysis is a resource-demanding feature - use it with caution on large solutions.

Use the Remove Unused Members Code Cleanup Rule

The Remove unused members cleanup rule allows you to remove unused private members from code.

In the example below, two private methods are never used.

public class MyClass  {
    private void UsedMethod() {
    }

    private void UnusedMethod1() {
    }

    private void UnusedMethod2() {
    }

    public void MyMethod() {
        UsedMethod();
    }
}

Follow the following steps to remove the unused methods:

  • Open the Editor | C# (Visual Basic) | Code Cleanup options page.

  • Select the "Remove unused members" rule and enable the "Apply in Action" checkbox. This allows CodeRush to apply this cleanup rule when you run code cleanup.

    EnableCleanup

  • Click OK to save and apply the settings.

  • Run Code Cleanup.

The screencast below shows the applied code cleanup:

Cleanup