coderushforroslyn-402108-getting-started-examples-refactorings-how-to-remove-unused-members-from-code.md
This example describes ways to remove unused members from code.
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;
}
}
Module TestModule
Public a0 As Double = 0.938229
Public a1 As Double = -0.726534
Public a2 As Double = 0.647538
Public a3 As Double = 0.382564
Private a4 As Double = 0.345623
Sub Main()
Dim input As String
Dim x, y As Double
Do
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 e1 As FormatException
Exit Do
End Try
Loop
End Sub
End Module
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.
After execution, this refactoring removes the a4 variable from the code.
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.
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.
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();
}
}
Public Class MyClass
Private Sub UsedMethod()
End Sub
Private Sub UnusedMethod1()
End Sub
Private Sub UnusedMethod2()
End Sub
Public Sub MyMethod()
UsedMethod()
End Sub
End Class
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.
Click OK to save and apply the settings.
Run Code Cleanup.
The screencast below shows the applied code cleanup: