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

Declare Local

  • 2 minutes to read

Purpose

This Code Provider generates a local variable for the current reference. You can generate an explicitly typed variable or implicitly typed variable. You can also declare a local variable in JavaScript and TypeScript code.

Availability

Available when the caret is on an undeclared variable reference.

Usage

  1. Place the caret on a reference to undeclared variable.

    NOTE

    The blinking cursor shows the caret's position at which the Code Provider is available.

double CalculateDiscount(double price) {
    double fraction = percentage / 100d;
    return price - fraction * price;
}
  1. Use the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions Menu.

  2. Use the mouse to select Declare | Declare Local from the menu and press Enter.

DeclareLocal

NOTE

Depending on the Local declaration style Code Style Rule, the variable will be declared implicitly or explicitly.

After execution, the Code Provider declares a new local variable of the appropriate type and puts the caret on its initial value. The new local variable is set to 0 by default.

double CalculateDiscount(double price) {
    double percentage = 0d;
    double fraction = percentage / 100d;
    return price - fraction * price;
}