Skip to main content

Remove Variable

Purpose

Removes a variable that is never used in code.

Availability

Available when the cursor is in an unused variable.

How to Use

  1. Place the caret in an unused variable. For example, in the “methodSymbol” variable.

    public class RemoveUnusedVariable {
        string GetMethodName(ISymbol symbol) {
            if (symbol is IMethodSymbol methodSymbol) {
                return symbol.Name;
            }
            return null;
        }
    }
    
  2. Press the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu.

    RemoveVariable

  3. Select Unused Local -> Remove ‘xxx’ Variable from the menu.

After execution, the Refactoring removes the unused variable.

public class RemoveUnusedVariable {
    string GetMethodName(ISymbol symbol) {
        if (symbol is IMethodSymbol) {
            return symbol.Name;
        }
        return null;
    }
}
See Also