Skip to main content

Remove Variable with its Initializer

Purpose

Removes an unused variable with its initializer from code.

Availability

Available when the cursor is in an initialized variable. This variable is never used in code and can be deleted.

How to Use

  1. Place the caret in an initialized variable. For example, in the “statementsList” variable.

    class RemoveVariable {
        private BlockSyntax GetGetterBody(IMemberDescriptor field) {
            var statementsList = new List<StatementSyntax>();
            return SyntaxFactory.Block(new StatementSyntax[] { FieldToReturnStatement(field) });
        }
    }
    
  2. Press the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu.

    RemoveVariable

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

After execution, the Refactoring removes the unused variable with its initializer.

class RemoveVariable {
    private BlockSyntax GetGetterBody(IMemberDescriptor field) {
        return SyntaxFactory.Block(new StatementSyntax[] { FieldToReturnStatement(field) });
    }
}
See Also