Declare Local

  • 3 minutes to read

Purpose

This Code Provider generates a local variable for the current reference. You can generate an explicitly or implicitly typed variable.

Availability

Available when the caret is in an undeclared variable reference.

Usage

  1. Place the caret in 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. Select Declare | Declare Local from the menu and press Enter.

    DeclareLocal

NOTE

You can declare the variable implicitly or explicitly depending on the Local declaration style. Refer to the Code Style Rule topic for more information.

The red target picker marker is disabled for this code provider. This target picker allows you to choose the place where the generated local variable can be inserted.

You can configure the target picker on the Editor | All Languages | Code Actions | Target Picker options page.

TargetPicker

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.

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

Blazor Support

You can run the Declare Local code provider from the code sections in .razor files:

DeclareLocal