Skip to main content
A newer version of this page is available. .

Discard Variable

Purpose

Replaces a variable that is never used in code with discard.

Availability

Available when the cursor is in an unused variable. This variable can be replaced with discard.

How to Use

  1. Place the caret in an unused variable.
NOTE

The blinking cursor shows the caret's position where Refactoring is available.

public static class DiscardVariable {
    public static bool HasAssignment(this SimpleNameSyntax identifier) {
        SyntaxNode assignment;
        return identifier.HasAssignment(out assignment);
    }
}
  1. Use the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions Menu.

RemoveVariable

  1. Select Unused Local -> Discard 'xxx' from the menu.

After execution, the Refactoring replaces the unused local variable with discard.

public static class DiscardVariable {
    public static bool HasAssignment(this SimpleNameSyntax identifier) {
        return identifier.HasAssignment(out _);
    }
}